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 6 of 7

After studying Cats2's Cat class, you might have the impression that abstract classes can declare only abstract methods. However, abstract classes can also declare constructors, other nonabstract methods, and fields, as shown in Listing 6:

Listing 6. Cats3.java

// Cats3.java
abstract class Cat
{
   private String name;
   Cat (String name)
   {
      this.name = name;
   }
   String getName ()
   {
      return name;
   }
   abstract String getInfo ();
}
class Lion extends Cat
{
   Lion (String name)
   {
      super (name);
   }
   String getInfo ()
   {
      return "king of the jungle";
   }
}
class Cheetah extends Cat
{
   Cheetah (String name)
   {
      super (name);
   }
   String getInfo ()
   {
      return "great speed";
   }
}
class Tiger extends Cat
{
   Tiger (String name)
   {
      super (name);
   }
   String getInfo ()
   {
      return "has stripes";
   }
}
class Cats3
{
   public static void main (String [] args)
   {
      Cat [] cats =
      {
         new Lion ("Donald"),
         new Cheetah ("Lucifer"),
         new Tiger ("Xena"),
//         new Cat ()
      };
      for (int i = 0; i < cats.length; i++)
           System.out.println (cats [i].getName () + " " +
                               cats [i].getInfo ());
   }
}


Cats3 declares a Cat class with an instance field (name), a constructor that initializes that field, and a getter method (getName()) that returns that field's value. (The name field and its getter method appear in Cat because all cats have names -- and we want to factor out commonality to prevent redundancy.) Despite the presence of the Cat(String name) constructor, you cannot create an object from the abstract Cat class. So why have that constructor? Notice that each subclass declares a constructor that passes its name parameter value to the Cat superclass constructor through the super (name); superclass constructor call. Cat's constructor initializes Cat's private name field because subclasses can't access a superclass's private fields. When we create a Lion, Cheetah, or Tiger object, we want to ensure that each object's internal Cat layer properly initializes. That often happens by having the subclass constructor pass information from its constructor to the superclass constructor.

Abstract classes versus interfaces

Developers often inquire about the difference between abstract classes and interfaces. After all, you can declare an abstract class containing only abstract methods and/or constants, which is exactly what an interface contains. Do these two language features differ?

Abstract classes belong to class hierarchies. They represent generic entity categories and typically sit at the top of such hierarchies. Furthermore, developers often factor out commonality from more specific subclasses and place that commonality in less specific (and abstract) superclasses. For example, in Cat3, the Cat class represents the generic cat category: Cat presents that state and those behaviors common to all cats. (Obviously, Cat is not complete.)

In contrast, interfaces typically represent capabilities common to diverse class hierarchies; they do not represent entity categories. For example, in DMB2, the StartStop interface factors out the capabilities of starting and stopping entities. (DMB2 allows conference and vehicle entities to start and stop.)

  • Print
  • Feedback

Resources