Polymorphism PDF
Polymorphism PDF
OOP: Polymorphism 1
Class Hierarchies in Java, Revisited
• Class Object is the root of the inheritance hierarchy in Java.
• If no superclass is specified a class inherits implicitly from
Object.
• If a superclass is specified explicitly the subclass will inherit
indirectly from Object.
Object
Shape
Square
OOP: Polymorphism 2
Why Polymorphism?
// substitutability
Shape s;
s.draw() Shape
s.resize()
Circle Line Rectangle
// extensibility
Shape s;
s.draw() Shape
s.resize()
Circle Line Rectangle
Square
OOP: Polymorphism 3
Why Polymorphism?, cont.
// common interface
Shape s;
s.draw() Shape
s.resize() draw()
resize()
Downcast
Upcast
Circle Line Rectangle
draw() draw() draw()
resize() resize() resize()
// upcasting
Shape s = new Line();
s.draw()
s.resize()
OOP: Polymorphism 4
Advantages/Disadvantages of Upcast
• Advantages
Code is simpler to write (and read)
Uniform interface for clients, i.e., type specific details only in class code,
not in the client code
Change in types in the class does not effect the clients
If type change within the inheritance hierarchy
• Disadvantages
Must explictely downcast if type details needed in client after object has
been handled by the standard library (very annoing sometimes).
Shape s = new Line();
Line l = (Line) s; // downcast
OOP: Polymorphism 5
Static and Dynamic Type
• The static type of a variable/argument is the declaration type.
• The dynamic type of a variable/argument is the type of the object
the variable/argument refers to.
class A{
// body
}
class B extends A{
// body
}
public static void main(String args[]){
A x; // static type A
B y; // static type B
OOP: Polymorphism 6
Polymorphism, informal
• In a bar you say “I want a beer!”
What ever beer you get is okay because your request was very generic
• In a bar you say “I want a Samuel Adams Cherry Flavored beer!”
If you do not exactly get this type of beer you are allowed to complain
OOP: Polymorphism 7
Polymorphism
• Polymorphism: “The ability of a variable or argument to refer at
run-time to instances of various classes” [Meyer pp. 224].
Shape s = new Shape();
Circle c = new Circle();
Line l = new Line();
Rectangle r = new Rectangle();
s = l; // is this legal?
l = s; // is this legal?
l = (Line)s // is this legal?
}
public class Base{ //prints
public static void main(String[] args){ A()
B b = new B(); B.doStuff() 0
b.doStuff(); B()
}
} B.doStuff() 7
OOP: Polymorphism 11
Polymorphish and private Methods
class Shape {
void draw() { System.out.println ("Shape"); }
private void doStuff() {
System.out.println("Shape.doStuff()");
}
}
class Rectangle extends Shape {
void draw() {System.out.println ("Rectangle"); }
public void doStuff() {
System.out.println("Rectangle.doStuff()");
}
}
OOP: Polymorphism 13
Overloading vs. Polymorphism (1)
• Has not yet discovered that the Circle, Line and Rectangle classes
are related. (not very realisitic but just to show the idea).
Circle
draw()
resize()
Line
OverloadClient draw()
resize()
Rectangle
draw()
resize()
Usage not
inheritence
OOP: Polymorphism 14
Overloading vs. Polymorphism (2)
class Circle {
void draw() { System.out.println("Circle"); }}
class Line {
void draw() { System.out.println("Line"); }}
class Rectangle {
void draw() { System.out.println("Rectangle"); }}
Shape
PolymorphClient draw()
resize()
OOP: Polymorphism 16
Overloading vs. Polymorphism (4)
class Shape {
void draw() { System.out.println("Shape"); }}
class Circle extends Shape {
void draw() { System.out.println("Circle"); }}
class Line extends Shape {
void draw() { System.out.println("Line"); }}
class Rectangle extends Shape {
void draw() { System.out.println("Rectangle"); }}
Circle
draw()
resize()
Line
draw()
resize()
OverloadClient
Rectangle
draw()
resize()
Square
draw()
resize()
OOP: Polymorphism 18
Overloading vs. Polymorphism (6)
class Circle {
void draw() { System.out.println("Circle"); }}
class Line {
void draw() { System.out.println("Line"); }}
class Rectangle {
void draw() { System.out.println("Rectangle"); }}
class Square {
void draw() { System.out.println("Square"); }}
Shape
PolymorphClient draw()
resize()
Square
draw()
resize()
OOP: Polymorphism 20
Overloading vs. Polymorphism (8)
class Shape {
void draw() { System.out.println("Shape"); }}
class Circle extends Shape {
void draw() { System.out.println("Circle"); }}
class Line extends Shape {
void draw() { System.out.println("Line"); }}
class Rectangle extends Shape {
void draw() { System.out.println("Rectangle"); }}
class Square extends Rectangle {
void draw() { System.out.println("Square"); }}
• Closed
The new classes added do not affect old clients.
The superclass interface of the new classes can be used by old clients.
OOP: Polymorphism 22
A Polymorph Field
• A scientist does three very different things
Writes paper (and drinking coffee)
Teaches classes (and drinking water)
Administration (and drinking tea)
• The implementation of each is assumed very complex
• Must be able to change dynamically between these modes
Scientist Mode
work()
mode
drink()
OOP: Polymorphism 23
Implementing a Polymorph Field
public class Mode{
public void work(){ System.out.println("");}
public void drink(){ System.out.println("");}
}
OOP: Polymorphism 24
Implementing a Polymorph Field, cont.
public class Scientist{
private Mode mode;
public Scientist(){
mode = new WriteMode(); /* default mode */
}
// what scientist does
public void doing() { mode.work();}
public void drink() { mode.drink();}
OOP: Polymorphism 26
Abstract Class and Method
• An abstract class is a class with an abstract method.
• An abstract method is method with out a body, i.e., only declared
but not defined.
OOP: Polymorphism 27
Abstract Class and Method, Example
Abstract Concrete
C1 A
d1 Abstract class C1 with
B abstract methods A and B
d2
C
OOP: Polymorphism 28
Abstract Classes in Java
abstract class ClassName {
// <class body>
}
OOP: Polymorphism 29
Abstract Class in Java, Example
// [Source: Kurt Nørmark]
public abstract class Stack{
OOP: Polymorphism 30
Abstract Methods in Java
abstract [access modifier] return type
methodName([parameters]);
• You are saying: “The object should have this properties I just do
not know how to implement the property at this level of
abstraction.”
OOP: Polymorphism 31
The Composite Design Pattern
Client
use
Component
print()
add()
remove()
components
OOP: Polymorphism 34
Summary
• Polymorphism an object-oriented “switch” statement.
• Polymorphism should strongly be prefered over overloading
Must simpler for the class programmer
Identical (almost) to the client programmer
• Polymorphism is a prerequest for dynamic binding and central to
the object-oriented programming paradigm.
Sometimes polymorphism and dynamic binding are described as the
same concept (this is inaccurate).
• Abstract classes
Complete abstract class no methods are abstract but instatiation does not
make sense.
Incomplete abstract class, some method are abstract.
OOP: Polymorphism 35
Abstract Methods in Java, Example
public abstract class Number {
public abstract int intValue();
public abstract long longValue();
public abstract double doubleValue();
public abstract float floatValue();
public byte byteValue(){
// method body
}
public short shortValue(){
// method body
}
}
OOP: Polymorphism 36