









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










CMSC 131 Spring 2007
Bonnie Dorr (adapted from Rance Cleaveland)
CMSC 131 Spring 2007
Bonnie Dorr (adapted from Rance Cleaveland)
Safe Downcasting z
Illegal downcasting results in a thrown
ClassCastException
at run-time
Q: Can we check for the legality of a cast before trying it?
A: Yes, using
instanceof
Example z
Given:
ArrayList
of university people
z
Want: Print the GPAs of the students
z
Solution approach z
Iterate through list
z
Print GPAs only of Student
z
Recall the following
Iterator
methods:
z
hasNext()
z
next()
CMSC 131 Spring 2007
Bonnie Dorr (adapted from Rance Cleaveland)
Object andUpcasting/Downcasting (Recap) z
All objects are derived (directly or indirectly) from Object
z
Late binding and inheritance allows you to create polymorphicvariables
z
When a method in a base class is not provided, the method andclass are said to be abstract. Abstract methods may beimplemented in (concrete) derived classes
Run-time information about class / type information of objectscan be obtained using
getClass()
method and
instanceof
operator
Upcasting of object to superclass type is always safe and doneautomatically
Downcasting may not be safe, depending on actual class objectbelongs to at run-time. Safe downcasting can be done using instanceof
CMSC 131 Spring 2007
Bonnie Dorr (adapted from Rance Cleaveland)
Abstract Classes vs. Interfaces z
Interfaces seem like “pure abstract classes” z
Interfaces contain method prototypes
These are similar to abstract methods
z
Abstract classes permit some methods to be definedand shared Advantage: abstract classes
z
A given class can match multiple interfaces, but canonly inherit from one class Advantage: interfaces
z
Which to use? z
Code to share: use abstract classes
Otherwise: interfaces
CMSC 131 Spring 2007
Bonnie Dorr (adapted from Rance Cleaveland)
equals()
Reconsidered
z
Recall definition of
equals()
z
return name.equals(p.getName()) &&
idNum.equals(p.getIdNum());
z
Student s ) {
return super.equals(s) &&
admitYear == s.admitYear && gpa == s.gpa;
z
What does following do?
public static void main (String[] args) {
Student bob = new Student ("R. Goode", "234-56-7890",
Faculty bob2 = new Faculty ("R. Goode", "234-56-7890", 2005); System.out.println
(bob.equals (bob2));
z
true
is printed!
CMSC 131 Spring 2007
Bonnie Dorr (adapted from Rance Cleaveland)
Why? z
bob
“is-a”
Student
z
bob
“is-a”
Faculty
z
Both “are-a”
Person
z
bob
cannot be cast to
Student
z
bob
can be cast to
Person
z
So the
Person
version of
equals()
in
bob
is
called (overloading!)
z
But how can objects in two different classesbe equal?
CMSC 131 Spring 2007
Bonnie Dorr (adapted from Rance Cleaveland)
Copy ConstructorsReconsidered z
Copy constructors used to make copies ofobjects
z
Recall copy constructor for Person
public Person( Person p ) {
name = p.name; idNum = p.idNum;
}
z
Does this always do what we want?
CMSC 131 Spring 2007
Bonnie Dorr (adapted from Rance Cleaveland)
Deep Copying of a
Person
Array
public static Person[] deepCopy (Person[] a) {
Person[] r = new Person[a.length]; for (int
i=0; i < a.length; i++) r[i] = new Person(a[i]);
return r;
}
z
What happens if
a
contains
Student
,^
Faculty
objects?
z
They are converted into
Person
objects; extra info lost
z
a[0] = new Student (“BG”,“123-45-6789”,2005,3.2);
z
a[1] = new Faculty (“FS”,“111-11-1111”,2003);
z
z
r[0]
is
Person
object with
name==“BG”,
idNum == “123-45-6789”
z
r[1]
is
Person
object with
name==“FS”,
idNum == “111-11-1111”
z
Why? z
z
z
CMSC 131 Spring 2007
Bonnie Dorr (adapted from Rance Cleaveland)
Defining clone in Person
public Person clone () {
return new Person(this);
}
this
used to refer to current object
Copy constructor called on
this
Similar
clone()
methods
definable for
Student,
Faculty
New
deepCopy()
method:
public static Person[] deepCopy (Person[] a) {
Person[] r = new Person[a.length]; for (int i=0; i < a.length; i++)
r[i] = a[i].clone();
return r;
}
CMSC 131 Spring 2007
Bonnie Dorr (adapted from Rance Cleaveland)
equals()
and
clone()
:
A Summary z
When class inheritance, you may not know theactual classes of objects that you are manipulating.This complicates your job as a programmer.
z
Set up your method parameters to be of the mostgeneral type that is applicable to your method (e.g. Object
or
Person
rather than
Student
or
Faculty
).
z
Whenever dealing with references to objects in theclass hierarchy, use method overriding to producethe proper behavior. (E.g. using
clone()
rather
than a copy constructor).
CMSC 131 Spring 2007
Bonnie Dorr (adapted from Rance Cleaveland)
Example: Shapes
public abstract
class Shape
// Abstract class
private int
color;
// Color of shape
} public class Circle
extends Shape
// Concrete class
private double radius;
// Radius of circle
private Point center
// Center of circle
} public class Rectangle
extends Shape
// Concrete class
private Point upperLeft
// Upper left corner
private Point lowerRight
// Lower right corner
…
}
z
Each
Circle, Rectangle
“is-a”
Shape
(
inheritance
)
z
Each
Circle
“has-a”
Point
(
center
)
(composition)
z
Each
Rectangle
“has-a” two
Point
s (
upperLeft, lowerRight
)
CMSC 131 Spring 2007
Bonnie Dorr (adapted from Rance Cleaveland)
When To Use Inheritance,Composition? z
Ask “is-a” / “has-a” question
z
Answer = “is-a”: inheritance
z
Answer = “has-a”: composition