Recommended: Sing it, brah! 5 fabulous songs for developers
JW's Top 5
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 4 of 7
Listing 3. DMB2.java
// DMB2.java
interface StartStop
{
void start ();
void stop ();
}
class Conference implements StartStop
{
public void start ()
{
System.out.println ("Start the conference.");
}
public void stop ()
{
System.out.println ("Stop the conference.");
}
}
class PoliticalConvention extends Conference
{
public void start ()
{
System.out.println ("Introduce the guest speaker.");
}
public void stop ()
{
System.out.println ("Grab some refreshments.");
}
}
class Vehicle implements StartStop
{
public void start ()
{
}
public void stop ()
{
}
}
class Car extends Vehicle
{
public void start ()
{
System.out.println ("Insert key in ignition and turn.");
}
public void stop ()
{
System.out.println ("Turn key in ignition and remove.");
}
}
class LawnMower extends Vehicle
{
public void start ()
{
System.out.println ("Move sliding switch to on and pull cord.");
}
public void stop ()
{
System.out.println ("Move sliding switch to off.");
}
}
class DMB2
{
public static void main (String [] args)
{
Vehicle v = new LawnMower ();
StartStop [] ss =
{
new Car (),
v,
new PoliticalConvention (),
new Conference ()
};
for (int i = 0; i < ss.length; i++)
{
ss [i].start ();
ss [i].stop ();
System.out.println ("");
}
}
}
DMB2 introduces a pair of class hierarchies. The Conference and Vehicle root classes in each hierarchy implement the StartStop interface, and the subclasses override the respective implementations of StartStop's methods.
The main() method in the code above creates a LawnMower object and assigns its reference to Vehicle's object reference variable v. Then, main() creates an array of references to various objects, whose classes explicitly or implicitly (through a superclass) implement
StartStop -- with one of the array's elements including v's contents. A for statement loops over those array elements, and each iteration calls an object's start() and stop() methods. To make the right call, the JVM locates the correct method by examining and following each array element's object
reference to the object's memory area. There, the JVM can obtain the method's bytecode instructions.
When run, DMB2 produces the following output:
Insert key in ignition and turn. Turn key in ignition and remove. Move sliding switch to on and pull cord. Move sliding switch to off. Introduce the guest speaker. Grab some refreshments. Start the conference. Stop the conference.
Inclusion/subtype polymorphism is a natural part of Java. The underlying dynamic method-binding mechanism that initiates this form of polymorphism kicks into action whenever you call an instance method, regardless of whether an object reference variable has class or interface type. Nevertheless, to achieve inclusion/subtype polymorphism's full benefits, you must work with class hierarchies, where subclasses introduce methods that override superclass methods.
Like inclusion/subtype polymorphism, abstract classes are useful in the context of class hierarchies. When you design a class
hierarchy, classes near the top of that hierarchy typically represent generic concepts. As such, you do not create objects
from those classes; instead, you normally create objects from more specialized subclasses towards the hierarchy's bottom.
Although you probably know which classes in the hierarchy to use for object creation and which ones to avoid, how do you prevent
other developers from creating objects from the top classes in the hierarchy (assuming you distribute that hierarchy's classes
-- as classfiles -- to other developers)? Permitting such behavior would allow developers to create meaningless objects, as
Listing 4's Cats1 application demonstrates:
switch statements start reappearing in your code in "Java Tip 30Polymorphism and Java," Philip Bishop (JavaWorld): http://www.javaworld.com/javaworld/javatips/jw-javatip30.html