Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

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

Object-oriented language basics, Part 7

Learn about Java's many shapes and find out how to accommodate generalities in your class hierarchies

  • Print
  • Feedback

Page 7 of 7

When you properly combine abstract classes and interfaces, you achieve powerful program architectures, as shown in Listing 7:

Listing 7. DMB3.java

// DMB3.java
interface StartStop
{
   void start ();
   void stop ();
}
abstract class Conference implements StartStop
{
}
class PoliticalConvention extends Conference
{
   public void start ()
   {
      System.out.println ("Introduce the guest speaker.");
   }
   public void stop ()
   {
      System.out.println ("Grab some refreshments.");
   }
}
abstract class Vehicle implements StartStop
{
   private String manufacturer;
   Vehicle (String manufacturer)
   {
      this.manufacturer = manufacturer;
   }
   String getManufacturer ()
   {
      return manufacturer;
   }
}
class Car extends Vehicle
{
   Car (String manufacturer)
   {
      super (manufacturer);
   }
   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
{
   LawnMower (String manufacturer)
   {
      super (manufacturer);
   }
   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 DMB3
{
   public static void main (String [] args)
   {
      Vehicle v = new LawnMower ("Black and Decker");
      StartStop [] ss =
      {
         new Car ("General Motors"),
         v,
         new PoliticalConvention (),
      };
      for (int i = 0; i < ss.length; i++)
      {
           ss [i].start ();
           ss [i].stop ();
           if (ss [i] instanceof Vehicle)
               System.out.println ("Vehicle manufacturer = " +
                                   ((Vehicle) ss [i]).getManufacturer ());
           System.out.println ("");
      }
   }
}


DMB3 resembles DMB2 in that DMB3 also introduces two class hierarchies. One hierarchy deals with conferences, and the other deals with vehicles. However, unlike DMB2, DMB3 declares Conference and Vehicle as abstract. (Conference is abstract because it represents a generic conference concept, instead of something more specific, like a political conference. Vehicle is abstract for similar reasons.) Because those classes are abstract, they do not provide code bodies for start() and stop(). (Note: Though Conference and Vehicle do not provide code bodies for StartStop's methods, those classes still implement the StartStop interface because they inherit that interface's abstract methods.)

In addition to reviewing DMB3's abstract classes and methods, look carefully at DMB3's main() method. That method demonstrates a use for the instanceof operator. In addition to starting and stopping each vehicle and conference object, DMB3 determines whether the object is a type of vehicle by using instanceof. If an object is a vehicle, that object casts to Vehicle. Then, DMB3 calls the object's getManufacturer() method to retrieve the name of the vehicle's manufacturer, which it subsequently prints.

As you have just learned, abstract classes and interfaces can combine to form powerful program architectures. Abstract classes sit at the top of their respective class hierarchy pillars and factor out commonalities from within their respective hierarchies. Typically, abstract classes implement interfaces that extract common capabilities from each hierarchy. In some programs, those abstract classes provide code bodies for interface methods. In other programs, like DMB3, they do not.

Note: Recently, JavaWorld published an article by Wm. Paul Rogers that provides great clarity on when to use interfaces, when to use abstract classes, and when to combine those language features. I strongly recommend that you read "Maximize Flexibility with Interfaces and Abstract Classes."

Review

This article identified four kinds of polymorphism -- coercion, overloading, parametric, and inclusion -- and pointed out that Java does not officially support parametric polymorphism. I specifically focused on inclusion polymorphism, also called subtype polymorphism, and showed (through examples) that Java supports inclusion polymorphism by dynamically binding instance method calls to objects at runtime.

I then turned to abstract classes and described their usefulness in accommodating class hierarchy generalities. Finally, I compared abstract classes to interfaces and showed that both language features can combine to create powerful program architectures.

I encourage you to email me with any questions you might have involving either this or any previous article's material. (Please keep such questions relevant to material discussed in this column's articles.) Your questions and my answers will appear on the relevant study guide page.

In next month's article, you'll learn how to initialize objects and classes.

Note: To help you further master the Java language, I have enhanced Java 101 with a study guide that will accompany each column. Each study guide provides additional material relevant to its companion Java 101 article. In it you will find a glossary, homework, homework solutions, my answers to your questions, tips and cautions, and other material.

This article is the first to have its own study guide; future and, eventually, prior Java 101 articles will also feature their own guides.

About the author

Jeff Friesen has been involved with computers for the past 20 years. He holds a degree in computer science and has worked with many computer languages. Jeff has also taught introductory Java programming at the college level. In addition to writing for JavaWorld, he wrote his own Java book for beginners -- Java 2 By Example (QUE, 2000) -- and helped write a second Java book, Special Edition Using Java 2 Platform (QUE, 2001). Jeff goes by the nickname Java Jeff (or JavaJeff). To see what he's working on, check out his Website at http://www.javajeff.com.
  • Print
  • Feedback

Resources