Test 2 Flashcards
Class declaration
As in the last lecture, the class declaration
starts by specifying the class name
public class Rectangle
However, we also want to compare Rectangle
objects (e.g., to sort them)
public class Rectangle implements
Comparable
Additional interfaces can be added in a
comma-separated sequence
Remember that attributes are declared as
access [static] [final] type name [= value];
We are choosing to declare private variables
for the height and width, leaving them
uninitialized
private int width;
private int height;
attributes
Remember that attributes are declared as
access [static] [final] type name [= value];
We are choosing to declare private variables
for the height and width, leaving them
uninitialized
private int width;
private int height;
Static attributes are typically initialized when
they are declared, non-static are initialized in
the constructor
An attribute’s scope is the entire class
The fully qualified way to reference an
attribute is with the this keyword
this.width or this.height
However, this can be omitted if the result is
unambiguous (e.g., attribute shadowing)
Constructors
Constructors are defined as follows: access ClassName(anyParameters) Like methods, they can be overloaded When a new object is created… ◦ Memory is allocated for the new object ◦ A constructor for the corresponding class is called ◦ Attributes are initialized
Copy constructor
Takes an object of the same class as a
parameter
public ClassName(ClassName other)
Creates a new object with attributes identical
to the passed object
Note that you do not need (and should not
declare) a copy constructor for an immutable
type, as the resulting object would be
redundant
constructor chaining
When a constructor invokes another
constructor it is called constructor chaining
To invoke a constructor in the same class you
use the this keyword
◦ If you do this then it must occur on the first line of
the constructor body
Used to reduce code duplication
Not always feasible
◦ E.g., if constructor can throw exceptions
Methods
are defined as follows: access returnType signature Methods can perform any operation on an object, but typically fall into categories: ◦ Accessors – return an attribute value ◦ Mutators – modify an attribute value ◦ Obligatory – satisfy superclass or interface requirements ◦ Class-specific – defined by purpose of class
Acessors
Allows access to (otherwise private) attribute values Naming convention: ◦ getX() for a non-Boolean attribute, named x ◦ isX() for a Boolean attribute, named x public int getWidth() { return this.width; } // similar for getHeight()
Mutators
Allows modification of (otherwise private) attribute values Naming convention: ◦ setX() for an attribute, named x public void setWidth(int width) { this.width = width; } // similar for setHeight()
validation using mutators
Instead of relying on preconditions, can use
mutators to validate argument values
◦ Throw an exception
◦ Return a Boolean value
For the Rectangle class, validate that any
width argument is non-negative
toString method
Provides a textual representation of the Rectangle
◦ E.g., “Rectangle of width 1 and height 2”
Default returns class name and memory address
For the Rectangle class:
public String toString()
{
return “Rectangle of width “ + this.width +
“ and height “ + this.height;
}
equals method
Evaluate object equality using attribute values
(i.e., object state)
Default compares memory address (like ==)
Implementing equals() is surprisingly hard
◦ “One would expect that overriding equals(), since it is a fairly
common task, should be a piece of cake. The reality is far from
that. There is an amazing amount of disagreement in the Java
community regarding correct implementation of equals().”
Angelika Langer, Secrets of equals() – Part 1
◦ http://www.angelikalanger.com/Articles/JavaSolutions/SecretsOfEquals/Equals.html
Our approach is consistent with many texts
The implementation of equals() used in the
notes and the textbook is based on the rule
that an instance can only be equal to another
instance of the same type
At first glance, this sounds reasonable and is
easy to implement using Object.getClass()
public final Class extends Object> getClass()
Returns the runtime class of an object.
instances of same state are equal
Recall that the value of the attributes of an object
define the state of the object
Two instances are equal if all of their attributes are equal
Recipe for checking equality of attributes
1. If the attribute type is a primitive type other than float
or double use ==
2. If the attribute type is float use Float.compare()
3. If the attribute type is double use Double.compare()
4. If the attribute is an array consider Arrays.equals()
5. If the attribute is a reference type use equals(), but
beware of attributes that might be null
equals() is
For reference values equals() is
1. Reflexive :
An object is equal to itself
x.equals(x) is true
2. Symmetric :
Two objects must agree on whether they are equal
x.equals(y) is true if and only if y.equals(x) is true
3. Transitive :
If a first object is equal to a second, and the second object
is equal to a third, then the first object must be equal to
the third
If x.equals(y) is true, and y.equals(z) is true, then
x.equals(z) must be true
Consistent :
Repeatedly comparing two objects yields the same
result (assuming the state of the objects does not
change)
5. x.equals(null) is always false
compareTo method
Required if your class implements the
Comparable interface (i.e., its object can be
compared, order, or sorted)
Compares this object with the specified object
for order
◦ Returns a negative integer if this object is less than
◦ Returns a positive integer if this object is greater than
◦ Returns zero if this object is equal to the passed one
Throws a ClassCastException if the specified
object type cannot be compared to this object
The sign of the returned int must flip if the
order of the two compared objects flip
if x.compareTo(y) > 0 then y.compareTo(x) < 0
if x.compareTo(y) < 0 then y.compareTo(x) > 0
if x.compareTo(y) == 0 then y.compareTo(x) == 0