Polymorphism Lecture 1.pps
Polymorphism Lecture 1.pps
Polymorphism Lecture 1.pps
int x;
x = 0;
1 2 3 4 5 6
x 0
7 8 9 10 11 12
Employee t;
t = new Employee();
public Employee()
{
name = “”;
pay = 0.0;
}
}
name=“” pay=0.0
}
14 Copyright © 2000-2002 Tom Hunter
OOP Basics
public class HourlyEmployee extends Employee
{
private double rate;
private int hours; Superclass
Subclass
public HourlyEmployee( double r, int h )
{
//Implicit call to Superclass constructor
rate = r;
hours = h;
}
}
15 Copyright © 2000-2002 Tom Hunter
public class Employee
{ public class Object
public Employee() {
{ public Object()
System.out.println( “Employee constructor”); {
} }
}
}
public HourlyEmployee()
{
// implicit (hidden) call to Superclass Constructor.
rate = 0.0;
hours = 0;
}
public HourlyEmployee()
{
super( “”, “” ); // Explicit call to Superclass Constructor
rate = 0.0;
hours = 0;
}
money = hour.calc_pay();
System.out.println( “Money=” + money );
}
}
money = salr.calc_pay();
System.out.println( “Money=” + money );
}
}
money = empl.calc_pay();
System.out.println( “Money=” + money );
}
}
money = empl.calc_pay();
System.out.println( “Money=” + money );
}
}
money = empl.calc_pay();
System.out.println( “Money=” + money );
}
}
• More on Polymorphism
problems
• Why? The Subclass can do many things the
Superclass cannot.
An HourlyEmployee is an Employee
(Subclass is a Superclass object)
but…
Circle c;
c = new Circle();
56
public class Point
{
} Now, we have a
Superclass
public class Circle extends Point
{
reference, pointing
} to a subclass
object.
public class Test Is this legal?
{ Is this useful?
public Test()
{
Point p = new Circle();
Circle c = p;
}
No ! A Circle
reference would
c expect many
methods that you
For this to even remotely won’t find in a
work, you would need to do a Point object.
“downcast” ?
59
A Case Study of Inheriting a Class
• We’re attempting to do this error by casting a
Subclass reference to a Superclass object.
p instanceof Circle
1.) Refer to a
Superclass object with a
Superclass reference.
This is routine
2.) Refer to a
Subclass object with a
Subclass reference.
3.) Refer to a
Subclass object with a
Superclass reference
4.) Refer to a
Superclass object with a
Subclass reference .
pay = supEmp.calcPay();
System.out.println( "Superclass pay = " + pay );
subHrEmp.setHourlyRate( 5.65 );
subHrEmp.setHoursWorked( 20 );
pay = subHrEmp.calc_Pay();
System.out.println( "Subclass pay = " + pay );
supEmp = subHrEmp;
pay = supEmp.calcPay();
We declare a reference to "Superclass
System.out.println( an object ofpay = "Employee
type + pay );
called }public
supEmp. However,
static we String[]
void main( instantiate an)
args
HourlyEmployee
{ object and assign the reference to the
DemoPolymorphism demo = new DemoPolymorphism();
Superclass Employee.
System.exit( This
0 ); works because—after all—
}
HourlyEmployee
} is an Employee.
77 Copyright © 2000-2002 Tom Hunter
public class DemoPolymorphism
{
double pay = 0;
public DemoPolymorphism()
{
Employee supEmp = new HourlyEmployee();
HourlyEmployee subHrEmp = new HourlyEmployee();
pay = supEmp.calcPay();
System.out.println( "Superclass pay = " + pay );
subHrEmp.setHourlyRate( 5.65 );
subHrEmp.setHoursWorked( 20 );
pay = subHrEmp.calc_Pay();
System.out.println( "Subclass pay = " + pay );
supEmp = subHrEmp;
pay = supEmp.calcPay();
System.out.println( "Superclass pay = " + pay );
}
public static void main( String[] args )
{
DemoPolymorphism demo = new DemoPolymorphism();
System.exit( 0 );
}
} 78 Copyright © 2000-2002 Tom Hunter
Superclass pay = 0.0
Subclass pay = 113.0
Superclass pay = 113.0
Press any key to continue . . .