8 Polymorphism
8 Polymorphism
Inheritance and
Polymorphism
(Continued)
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
isA relationships
Circle c = new Circle();
Object GeometricObject c = new Circle(); // Rectangle
Object c = new Circle(); // Rectangle
Circle Rectangle
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
Implicit casting:
GeometricObject o = new Circle();
Explicit casting:
Circle c = (Circle) o;
c.radius c.getArea()
((Circle) o).radius ((Circle) o).getArea()
if (o instanceof Circle)
{
((Circle) o).radius;
((Circle) o).getArea();
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
display (Object o)
{
From driver class/main:
if (o instanceof Circle)
{ display (new Circle());
((Circle) o).radius; display (new Rectangle());
((Circle) o).getArea();
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
@Override annotation
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
Polymorphism, Dynamic Binding
public class PolymorphismDemo {
public static void
main(String[] args) {
m(new GraduateStudent()); Method m takes a parameter
m(new Student()); of the Object type. You can
m(new Person());
m(new Object());
invoke it with any object.
}
An object of a subtype can be used wherever its
public static void m(Object x) { supertype value is required. This feature is
System.out.println(x.toString());
}
known as polymorphism.
}
When the method m(Object x) is executed,
class GraduateStudent extends Student
{ the argument x’s toString method is invoked. x
} may be an instance of GraduateStudent,
class Student extends Person {
Student, Person, or Object. Classes
public String toString() { GraduateStudent, Student, Person, and Object
return "Student"; have their own implementation of the toString
} method. Which implementation is used will be
}
determined dynamically by the Java Virtual
class Person extends Object { Machine at runtime. This capability is known
public String toString() { as dynamic binding.
return "Person";
}
} PolymorphismDemo
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
• The type of a variable is called its declared type. Here o’s
declared type is Object.
• The actual type of the variable is the actual class for the
object referenced by the ariable. Here o’s actual type is
GeometricObject, since o references to an object
created using new GeometricObject().
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
Dynamic Binding
Dynamic binding works as follows:
Suppose an object o is an instance of classes C1, C2, ..., Cn-1, and Cn,
where C1 is a subclass of C2, C2 is a subclass of C3, ..., and Cn-1 is a
subclass of Cn. That is, Cn is the most general class, and C1 is the
most specific class. In Java, Cn is the Object class. If o invokes a
method p, the JVM searches the implementation for the method p
in C1, C2, ..., Cn-1 and Cn, in this order, until it is found. Once an
implementation is found, the search stops and the first-found
implementation is invoked.
Cn Cn-1 ..... C2 C1
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
Generic programming
public class PolymorphismDemo {
public static void main(String[] args)
{ • Polymorphism allows methods to be
m(new GraduateStudent()); used generically for a wide range of
m(new Student());
m(new Person()); object arguments. This is known as
m(new Object()); generic programming.
}
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
Casting Objects
You have already used the casting operator to convert variables of
one primitive type to another. Casting can also be used to convert
an object of one class type to another within an inheritance
hierarchy. In the preceding section, the statement
m(new Student());
Student b = o;
Apple x = (Apple)fruit;
Orange x = (Orange)fruit;
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
TIP
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
The instanceof Operator
If the superclass object is not an instance of the subclass, a runtime
ClassCastException occurs.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
Example: Demonstrating Polymorphism and Casting
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
The equals Method
• The equals() method compares the contents of two objects. The
default implementation of the equals method in the Object class is as
follows. This implementation checks whether two reference variables
point to the same object using the == operator.
object1.equals(object2);
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
The ArrayList and Vector
Classes
You can create an array to store objects. But the array’s size is fixed
once the array is created. Java provides the ArrayList class that can
be used to store an unlimited number of objects.
java.util.ArrayList
+ArrayList() Creates an empty list.
+add(o: Object) : void Appends a new element o at the end of this list.
+add(index: int, o: Object) : void Adds a new element o at the specified index in this list.
+clear(): void Removes all the elements from this list.
+contains(o: Object): boolean Returns true if this list contains the element o.
+get(index: int) : Object Returns the element from this list at the specified index.
+indexOf(o: Object) : int Returns the index of the first matching element in this list.
+isEmpty(): boolean Returns true if this list contains no elements.
+lastIndexOf(o: Object) : int Returns the index of the last matching element in this list.
+remove(o: Object): boolean Removes the element o from this list.
+size(): int Returns the number of elements in this list.
+remove(index: int) : Object Removes the element at the specified index.
+set(index: int, o: Object) : Object Sets the element at the specified index.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
The protected Modifier
• The protected modifier can be applied on data and
methods in a class.
• A protected data or a protected method in a public
class can be accessed by any class in the same package
or its subclasses, even if the subclasses are in a
different package.
• private, default, protected, public
Visibility increases
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
Accessibility Summary
public
protected -
default - -
private - - -
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
Visibility Modifiers
package p1;
public class C1 { public class C2 {
public int x; C1 o = new C1();
protected int y; can access o.x;
int z; can access o.y;
private int u; can access o.z;
cannot access o.u;
protected void m() {
} can invoke o.m();
} }
package p2;
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
A Subclass Cannot Weaken the
Accessibility
• A subclass may override a protected
method in its superclass and change its
visibility to public.
• However, a subclass cannot weaken the
accessibility of a method defined in the
superclass. For example, if a method is
defined as public in the superclass, it
must be defined as public in the
subclass.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
NOTE
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
The final Modifier
• The final class cannot be extended:
• The final variable is a constant:
final static double PI = 3.14159;
• The final method cannot be
overridden by its subclasses.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807