Chapter 10: Polymorphism
Polymorphism
Polymorphism is an OO concept that allows us to create
versatile software designs
Chapter 10 focuses on:
CSc
152
defining polymorphism and its benefits
using inheritance to create polymorphic references
using interfaces to create polymorphic references
using polymorphism to implement sorting and searching
algorithms
additional GUI components
Polymorphism, Sorting and Searching
Binding
Consider this method call:
student.computeGrade();
At some point, this call is bound to the definition of the
method that it calls but when?
q
If at compile time, it would call the same method every time
If at runtime, it could be bound to a method "on the fly"
Java chooses the latter it's called dynamic binding or
late binding
Late binding is the basis for polymorphism provides
flexibility in program design
CSc
152
Polymorphism, Sorting and Searching
Polymorphism
Polymorphism literally means "having many forms"
A polymorphic reference is a variable that can refer to
different types of objects at different times
Which method is called through a polymorphic reference
can change from one call to the next
All object references in Java
are potentially polymorphic
CSc
152
Polymorphism, Sorting and Searching
Polymorphism
Given this declaration:
Occupation job;
Java allows job to point to:
q
an Occupation object, or
an object of any compatible type
This compatibility can be based on inheritance or
interfaces as we'll see
CSc
152
Polymorphism, Sorting and Searching
References and Inheritance
Consider this example
Bicycle
RoadBike
MountainBike
Suppose Bicycle has a method called printDescription
And that RoadBike and MountainBike override this method
with customized versions
CSc
152
Polymorphism, Sorting and Searching
References and Inheritance
Examine this code:
public class TestBikes
{
public static void main(String[] args)
{
Bicycle bike1, bike2, bike3;
bike1 = new Bicycle(20, 10, 1);
bike2 = new MountainBike(20, 10, 5, "Dual");
bike3 = new RoadBike(40, 20, 8, 23);
bike1.printDescription();
Who ya gonna call?
bike2.printDescription();
bike3.printDescription();
}
}
CSc
152
Polymorphism, Sorting and Searching
References and Inheritance
Bicycle
RoadBike
Widening
conversion
(no problem)
MountainBike
Bicycle bike3;
bike3 = new RoadBike (40, 20, 8, 23 );
Narrowing
conversion
(needs cast)
MountainBike bike2 ;
bike2 = (MountainBike) new Bicycle (20,10,1);
CSc
152
Polymorphism, Sorting and Searching
References and Inheritance
It is the type of the object being referenced, not the
reference type, that determines which method is invoked
public class TestBikes
{
public static void main(String[] args)
{
Bicycle bike1, bike2, bike3;
bike1 = new Bicycle(20, 10, 1);
bike2 = new MountainBike(20, 10, 5, "Dual");
bike3 = new RoadBike(40, 20, 8, 23);
bike1.printDescription();
bike2.printDescription();
bike3.printDescription();
}
}
CSc
152
Polymorphism, Sorting and Searching
Polymorphism via Inheritance
Consider the task of
StaffMember
(Abstract class)
paying employees:
Volunteer
Employee
Executive
Hourly
See Firm example (pg. 490-501)
CSc
152
Polymorphism, Sorting and Searching
10
Polymorphism via Interfaces
Suppose we have an interface called Speaker which
prescribes a method called speak
The interface name can be used as a type:
Speaker current;
The variable current can point to any object of any class
that implements Speaker
current.speak();
Who ya gonna call?
CSc
152
Which speak method is called
depends on the type of object
that current is pointing to at that
time.
Polymorphism, Sorting and Searching
11
Polymorphism via Interfaces
Suppose two classes, Philosopher and Dog, both
implement the Speaker interface, and provide distinct
versions of the speak method:
yada yada
Speaker guest = new Philospher ( );
guest.speak ( );
guest = new Dog ( );
guest.speak ( );
Squirrel !!
CSc
152
Polymorphism, Sorting and Searching
12
Sorting
Sorting arranges a list of items in a particular order
The sorting process is based on specific value(s)
q
Sort test scores in ascending numeric order
Sort people alphabetically by last name
Many sorting algorithms:
q
Vary in efficiency and complexity
Will look at two simple algorithms:
CSc
152
Selection Sort
Insertion Sort
Polymorphism, Sorting and Searching
13
Selection Sort
General approach:
q
Select a value and put it in its final place into the list
Repeat for all other values
Selection Sort Algorithm
find the smallest value in the list
switch it with the value in the first position
find the next smallest value in the list
switch it with the value in the second position
repeat until all values are in their proper places
CSc
152
Polymorphism, Sorting and Searching
14
Selection Sort
An example:
select
select smallest:
swap with 1st, select smallest:
swap with 2nd, select smallest:
swap with 3rd, select smallest:
6 ok (don't swap), only one left:
3
1
1
1
1
9
9
2
2
2
6
6
6
3
3
1
3
3
6
6
2
2
9
9
9
Each time, the smallest remaining value is found and
swapped with the element at the front of the unsorted part
of the list
CSc
152
Polymorphism, Sorting and Searching
15
Swapping
By "swap", we mean exchange the contents of two
variables (or array elements):
2
temp = blueBucket;
blueBucket = redBucket;
redBucket = temp;
3
1
temp
CSc
152
Polymorphism, Sorting and Searching
16
Have a Coke and a smile or whatever . . .
END OF THIS LECTURE!
CSc
152
Polymorphism, Sorting and Searching
17