Académique Documents
Professionnel Documents
Culture Documents
Programming - Polymorphism
Dr. Janusz Zalewski, Fall 2013
Prepared by Dr Dahai Guo
Outline
Introduction to Polymorphism
Superclass Reference and Subclass
Object
Abstract Classes and Methods
instanceof operator
Creating and Using Interfaces
class Fish
public void move()
class Mammal
public void move()
class Cat
public void move()
class Tortoise
public void move()
class Dog
public void move()
.
.
.
animal[99]
8. animals[i].move();
9. }
Question:
Do we still need to implement the
move method for class Animal?
class CommissionEmployee {
}
class BasePlusCommissionEmployee
extends CommissionEmployee {
}
CommissionEmployee employee
= new BasePlusCommissionEmployee();
3.
4.
5.
Illegal Statement!
Benefit of polymorphism.
Animal animals[ ] = new Animal[100];
1. /* creating 100 Animal-derived objects */
2.
3. for(int i=0; i<100;i++){
4.
animals[i].move();
5. }
Question:
If another Animal-based class is created, do we
need to change anything to the for loop?
class Fish
public void move()
class Mammal
public void move()
class Cat
public void move()
Difficult to define,
because only animal is
too abstract.
class Tortoise
public void move()
class Dog
public void move()
class Fish
public void move()
class Mammal
public void move()
class Cat
public void move()
class Tortoise
public void move()
class Dog
public void move()
class Fish
public void move()
class Cat
public void move()
class Tortoise
public void move()
class Cat
public void move()
SuperClass superRef;
superRef=new SubClass1();
superRef=new SubClass2();
superRef=new SubClass3();
superRef can be
used to point to
different subclass
objects.
HourlyEmployee
CommissionEmployee
SalariedEmployee
BasePlusCommissionEmployee
instanceof operator
1.
2.
3.
4.
5.
6.
Anything wrong?
HourlyEmployee employee=
new HourlyEmployee();
if(employee instanceof
BasePlusCommissionEmployee) {
System.out.println(BasePlusCommissionEmployee);
}
Downcasting
1. Employee employee =
2.
new BasePlusCommissionEmployee();
3.
4. if(employee instanceof
5.
BasePlusCommissionEmployee) {
6.
employee.setLastName(Johnson);
7.
((BasePlusCommissionEmployee)employee).
8.
9. }
setBaseSalary(1000);
Anything wrong?
1. interface MyInterface {
2. int i;
3. }
i is final, so it must be initialized at the declaration.
Anything wrong?
1. interface MyInterface {
2. protected int test();
3. }
Any interface methods must be public and abstract.
Invoice
Employee
HourlyEmployee
CommissionEmployee
SalariedEmployee
BasePlusCommissionEmployee
7.
}
8.
9. }
10. abstract class Employee implements Payable {
11.
// getPaymentAmount() not implemented
12. }
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
Equivalent?
abstract class Payable {
public abstract double getPaymentAmount();
}
class Invoice extends Payable {
public double getPaymentAmount() {
}
abstract class Employee extends Payable {
getPaymentAmount not implemented
}
Interfacex {
extends
New Program
implements
New Problem
extends
New
Sub-Class
Program
implements
New
Interface
Problem