Agenda
Homework review
Object identity and equality
Method overriding vs overloading
Tidy up
COMPSCI 230 Software
Construction
COMPSCI 230 Software Construction
Object equality
Object equality is
concerned with whether
the state of 2 distinct
objects is equal
Shape s1 = new Shape( 20, 20, 3, -5 );
Shape s2 = s1;
Shape s3 = new Shape( 20, 20, 3, -5 );
Shape s4 = new Shape( 20, 20, 3, -5 );
Shape instance
s1
x = 20
y = 20
deltaX = 3
deltaY = -5
s2
s1 and s2 identify the
COMPSCI
same instance
2
class Shape {
private int x, y;
private int deltaX, deltaY;
}
Identity vs equality
Object identity
Object identity is
determined by
reference (pointer)
equality
2
Shape instance
s3
x = 20
y = 20
deltaX = 3
deltaY = -5
s4
Shape instance
x = 20
y = 20
deltaX = 3
deltaY = -5
The objects referred to
230 Software
by s3 and s4 are equal
Construction
3 3
COMPSCI 230 Software Construction
Object
Object equality
equals( obj : Object ) : boolean
n OOPLs provide a means
Object obj1 = new Object( );
Object obj2 = obj1;
to test for object
equality
Object instance
obj1
n In Java the root
superclass, Object,
this
implements a method
obj2
named equals( ) which is
intended to be
obj1.equals( obj2 ); // => yields true
overridden by
subclasses
class Object {
n By default, Objects
public boolean equals( Object other )
{
equals( ) method tests
return this == other;
for pointer equality
}
}
COMPSCI 230 Software
Construction
4 4
COMPSCI 230 Software Construction
Object equality
class Shape {
private int x, y, deltaX, deltaY;
Parameter type must match that of
the method being overridden
// Constructor as before.
Ensure a null argument
doesnt cause a
NullPointerException
// Override method equals.
public boolean equals( Object other ) {
boolean result = false;
Check that the argument points
to some type of Shape object
if( ( other != null ) && other instanceof Shape ) {
Shape s = (Shape ) other;
result = x == s.x && y == s.y &&
s.deltaX == deltaX &&
s.deltaY == deltaY;
}
return result;
}
Cast to type Shape to access
Shape specific attributes
Compare values of this Shape
instances variables with those of
the arguments
}
}
COMPSCI 230 Software
Construction
COMPSCI 230 Software Construction
7
7
Method overloading
Method overloading is where a class contains
more than one method of the same name
String
indexOf( str : String ) : int
indexOf( str : String, fromIndex : int ) : int
In a statically typed OOPL like Java, a call to
an overloaded method is resolved at compile
time String
String myString = new String( Bounce.java );
myString.indexOf( . ) ;
COMPSCI 230
Software
indexOf( str : String ) : int
indexOf( str : String ) : Integer
Construction
COMPSCI 230 Software Construction
8
8
Constructor overloading
class Shape {
Constructors are
private int x, y, deltaX, deltaY;
commonly overloaded private static final int DELTA_X = 5;
private static final int DELTA_Y = 5;
Use of this is
Shape( int x, int y, int deltaX, int deltaY ) {
sometimes useful to public
this.x = x;
this.y = y;
distinguish between
this.deltaX = deltaX;
this.deltaY = deltaY;
constructors
}
public Shape( int x, int y ) {
this( x, y, DELTA_X, DELTA_Y );
}
}
COMPSCI 230 Software
Construction
COMPSCI 230 Software Construction
9
9
Key points
Object identity is determined by reference (pointer)
equality; do two pointers refer to the same object?
Object equality is concerned with object state; do
two objects have the same values for the instance
variables?
In Java, object equality is implemented by overriding
Objects equals( ) method; by default Object
implements equals( ) to carry out an identity test
Method overloading enables multiple versions of a
particular method to be defined
Overloaded methods are disambiguated by the number,
order and types of their arguments
Calls to overloaded methods are resolved at compile time
based
COMPSCI 230 Software
Construction
COMPSCI 230 Software Construction
1111
© Copyright 2026 Paperzz