Title: CSE 501N Fall 06 13: Polymorphism
1CSE 501NFall 0613 Polymorphism
2Lecture Outline
- Lab 3 questions
- Polymorphism
- Mid-semester review
3Polymorphism
- Polymorphism is an object-oriented concept that
allows us to create versatile software designs - We will focus on
- 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
4Polymorphism
- The term polymorphism literally means "having
many forms" - A polymorphic reference is a variable that can
refer to different types of objects at different
points in time - The method invoked through a polymorphic
reference can change from one invocation to the
next - All object references in Java are potentially
polymorphic
5Binding
- Consider the following method invocation
- obj.doIt()
- At some point, this invocation is bound to the
definition of the method that it invokes - If this binding occurred at compile time, then
that line of code would call the same method
every time - However, Java defers method binding until run
time -- this is called dynamic binding or late
binding - Late binding provides flexibility in program
design
6Polymorphism
- Suppose we create the following reference
variable - Occupation job
- Java allows this reference to point to an
Occupation object, or to any object of any
compatible type - This compatibility can be established using
inheritance or using interfaces - Careful use of polymorphic references can lead to
elegant, robust software designs
7Polymorphism
- Interface variable holds reference to object of a
class that implements the interfaceMeasurable
xNote that the object to which x refers
doesn't have type Measurable the type of the
object is some class that implements the
Measurable interface
x new BankAccount(10000)x new Coin(0.1,
"dime")
8Polymorphism
- You can call any of the interface methods
- Which method is called?
double m x.getMeasure()
9Polymorphism
- Depends on the actual object.
- If x refers to a bank account, calls
BankAccount.getMeasure - If x refers to a coin, calls Coin.getMeasure
- Behavior can vary depending on the actual type of
an object
10Polymorphism
- Called late binding resolved at runtime
- Different from overloading overloading is
resolved by the compiler (early binding)
11Polymorphism via Interfaces
- An interface name can be used as the type of an
object reference variable - Speaker current
- The current reference can be used to point to any
object of any class that implements the Speaker
interface - The version of speak that the following line
invokes depends on the type of object that
current is referencing - current.speak()
12Polymorphism via Interfaces
- Suppose two classes, Philosopher and Dog, both
implement the Speaker interface, providing
distinct versions of the speak method - In the following code, the first call to speak
invokes one version and the second invokes
another - Speaker guest new Philospher()
- guest.speak()
- guest new Dog()
- guest.speak()
13References and Inheritance
- An object reference can refer to an object of its
class, or to an object of any class related to it
by inheritance - For example, if the BankAccount class is used to
derive a class called SavingsAcct, then a
BankAccount reference could be used to point to a
SavingsAcct object
BankAccount acct acct new SavingsAcct()
14References and Inheritance
- Assigning a child object to a parent reference is
considered to be a widening conversion, and can
be performed by simple assignment - Assigning an parent object to a child reference
can be done also, but it is considered a
narrowing conversion and must be done with a cast - The widening conversion is the most useful
15Polymorphism
- As we have seen, in Java, type of a variable
doesn't completely determine type of object to
which it refers - Method calls are determined by type of actual
object, not type of object reference
BankAccount aBankAccount new SavingsAccount(1000
) // aBankAccount holds a reference to a
SavingsAccount
BankAccount anAccount new CheckingAccount()
anAccount.deposit(1000) // Calls "deposit"
from CheckingAccount
16Polymorphism
- Compiler needs to check that only legal methods
are invoked
Object anObject new BankAccount()
anObject.deposit(1000) // Wrong!
17Polymorphism
- Polymorphism ability to refer to objects of
multiple types with varying behavior - Polymorphism at work
- Depending on types of amount and other, different
versions of withdraw and deposit are called
public void transfer(double amount, BankAccount
other) withdraw(amount) // Shortcut for
this.withdraw(amount) other.deposit(amount)
18Polymorphism via Inheritance
- It is the type of the object being referenced,
not the reference type, that determines which
method is invoked - Suppose the BankAccount class has a method called
withdraw, and the SavingsAcct class overrides it - Now consider the following invocation
- acct.withdraw()
- If acct refers to a BankAccount object, it
invokes the BankAccount version of withdraw if
it refers to a SavingsAcct object, it invokes the
SavingsAcct version
19Polymorphism via Inheritance
- Consider the following class hierarchy
20Polymorphism via Inheritance
- Now let's look at an example that pays a set of
diverse employees using a polymorphic method - Example on the board
21Conclusion
- Exam on Wednesday
- Try to be here at exactly 530 so we can start on
time - Closed book, closed notes