Beruflich Dokumente
Kultur Dokumente
Chapter 9
Object-Oriented Programming:
Part II
• Mary got less than was promised. Java I--Copyright © 2000-2002 Tom Hunter
• Joe’s situation is akin to Referring to a
Subclass object with a Superclass
reference.
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. Java I--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 );
}
} Java I--Copyright © 2000-2002 Tom Hunter
Superclass pay = 0.0
Subclass pay = 113.0
Superclass pay = 113.0
Press any key to continue . . .
What a pain!
Java I--Copyright © 2000-2002 Tom Hunter
Polymorphism and Dynamic Binding
Polymorphism can save us that
trouble—and make programs that will work
with Shapes that hadn’t even been thought of
when the class was written.
Polymorphism.
• Declaring a method
static or
private
automatically makes it final .
Therefore:
“A class declared final cannot be
extended and every method is implicitly
final.”
Java I--Copyright © 2000-2002 Tom Hunter
Opposite of
final
class is an
abstract
Superclass
Java I--Copyright © 2000-2002 Tom Hunter
abstract Superclass
• Whereas a final class CANNOT ever be a
Superclass (meaning, no Subclass can ever inherit from it)
vs.
concrete class
C:\TestAbstract.java:15:
Whatiswent
class SubMustBe wrong? class.
an abstract
It can't
Well, be we didn’t override the abstract
instantiated.
since
SubMustBe
method smb = new we
doSomething(), SubMustBe();
inherited
^ our class
the abstract method into
2 errors
SubMustBe. Since we had a method that
was abstract, the whole class had to be
declared abstract.
Java I--Copyright © 2000-2002 Tom Hunter
abstract class MustBeAbstract
{
public abstract void doSomething();
}
class SubMustBe extends MustBeAbstract
{
public void doSomething()
{
}
}
public class TestAbstract
{
public static void main( String args[] )
{
SubMustBe smb = new SubMustBe();
System.exit( 0 );
}
}
• Now we have overridden the abstract
method and the compiler is happy.
Java I--Copyright © 2000-2002 Tom Hunter
abstract Superclass
Subclasses
ref = p; // Employee reference to a and assign them to
PieceWorker
output += ref.toString() + " earned $" + ref.earnings();
appropriate references for the
ref = h; // Employee reference to an HourlyWorker
various types of Employee.
output += ref.toString() + " earned $" + ref.earnings();
System.out.println( “” + output );
System.exit( 0 );
}
} Java I--Copyright © 2000-2002 Tom Hunter
public
We class each
reassign Test of these Employee Subclasses to the Superclass.
{
Using thestatic
public Superclass reference
void main( ref,
String we call
args[] ) the methods toString()
{ earnings() that also exist in the Superclass. The Runtime
and Employee ref; // superclass reference
identifies
Stringthat—in
output =fact—this
""; ref reference is really pointing to a
different kind of object. So the Runtime performs the correct overridden
Boss b = new Boss( "John", "Smith", 800.00 );
methods.
CommissionWorker c = new CommissionWorker( "Sue", "Jones", … 150);
PieceWorker p = new PieceWorker( "Bob", "Lewis", 2.5, 200 );
HourlyWorker h = new HourlyWorker( "Karen", "Price", 13.75, 40 );
System.out.println( “” + output );
System.exit( 0 );
}
} Java I--Copyright © 2000-2002 Tom Hunter
public class Test
{
public static void main( String args[] )
{
Employee ref; // superclass reference
String output = "";
This is
Boss b = new Boss( "John", "Smith", 800.00 );
CommissionWorker c = new CommissionWorker( "Sue", "Jones", … 150);
PieceWorker p = new PieceWorker( "Bob", "Lewis", 2.5, 200 );
Dynamic
HourlyWorker h = new HourlyWorker( "Karen", "Price", 13.75, 40 );
Method
ref = c; // Employee reference to a CommissionWorker
output += ref.toString() + " earned $” + ref.earnings();
Binding
ref = p; // Employee reference to a PieceWorker
output += ref.toString() + " earned $" + ref.earnings();
System.out.println( “” + output );
System.exit( 0 );
}
} Java I--Copyright © 2000-2002 Tom Hunter
A Payroll System using Polymorphism
• In the driver program, lines 140, 143 & 146 we see how
an array of type Shape has been declared, and each
element in the array has been initialized to hold a
different one of the Subclasses.
/* ActionListener.java 1.6 */
This is the actual code from Sun
import java.util.EventListener;
Microsystems that creates the
/** ActionListener
* The listener interface for receiving action events.
*
* @version 1.6 11/23/96
* @author Carl Quinn
*/
/* EventListener.java */
It gets lonely...
Out here...
on the heap...
alone.
class Inner
{
String innerThoughts = "My inner thoughts";
void doStuff()
{
// inner object has its own "this"
System.out.println( innerThoughts );
}
Java I--Copyright © 2000-2002 Tom Hunter
class TestMe
{
public static void main( String args[] )
{
// instantiate me, the outer object
Outer o = new Outer();
}
Java I--Copyright © 2000-2002 Tom Hunter
class TestMe
{
public static void main( String args[] )
{
// instantiate me, the outer object
Outer o = new Outer();
}
Java I--Copyright © 2000-2002 Tom Hunter
“Getting in Touch with Your Inner Class”
You can also instantiate both the outer class and inner
class at the same time:
• They are useful because they let you create a class close
to where they are needed and are being used—inside
another class.