Interfaces


Abstract Methods & Abstract Base Classes

An abstract method is one that appears in the body of a class declaration but omits the method body. A class that declares one or more abstract methods must be declared to be an abstract class. If you create an abstract method and forget to declare the class as being abstract the Java compiler will inform you of your mistake.

Now, you could simply declare a class to be abstract even though it provides implementations for all of its methods. This would prevent you from creating objects of the abstract class directly with the new operator. This may or may not be the intention of your application design goals. However, abstract classes of this nature are not the norm.

The Primary Purpose Of An Abstract Base Class

The primary purpose of an abstract base class is to provide a set of one or more public interface methods whose implementations are expected to be found in some derived class further down the inheritance hierarchy. The key phrase is “expected to be found in some derived class further down the inheritance hierarchy.” This means that as a designer you would employ an abstract class in your application architecture when you want a base class to specify rather than implement behavior and you expect derived classes to actually implement the behavior specified by the base class interface.

OK, why would you want to do this? Why create a class that does nothing but specify a set of interface methods? Good questions! The short answer is that abstract classes will comprise the upper tier(s) of your inheritance hierarchy. The upper tier(s) of an inheritance hierarchy is where you expect to find specifications for general behavior found in derived classes which comprise the lower tier(s) of an inheritance hierarchy. The derived classes, at some point, must provide implementations for those abstract methods specified in their base classes. Designing application architectures in this fashion — abstractions at the top and concrete implementations at the bottom — enables the architecture to be extended to accommodate new functionality rather than modified. This design technique injects a good dose of stability into your application architecture. This and other advanced object-oriented design techniques is discussed in more detail in chapter 24.

Expressing Abstract Base Classes In UML

Figure 11-10 shows a UML diagram that contains an abstract base class named AbstractClass.

image from book
Figure 11-10: Expressing an Abstract Class in the UML

Referring to figure 11-10 — the stereotype <<abstract>> is optional but if your draw your UML diagrams by hand it’s hard to write in italics, so, it will come in handy. Abstract classes can have fields and concrete methods like normal classes, but abstract methods are shown in italics.

Let’s now have a look at a short abstract class inheritance example.

Abstract Class Example

Figure 11-11 gives the UML class diagram for our example:

image from book
Figure 11-11: UML Class Diagram Showing the AbstractClass & DerivedClass Inheritance Hierarchy

Referring to figure 11-11 — AbstractClass has three methods and no fields. The first method is its default constructor and it is not abstract. (Constructors cannot be abstract!) The next two methods, printMessage() and setMessage() are shown in italics and are therefore abstract methods.

DerivedClass inherits from AbstractClass. Since AbstractClass’s methods are abstract, and have no implementation, DerivedClass must provide an implementation for them. DerivedClass’s methods are in plain font indicating they have implementations.

Now, if for some reason, you as a designer decided to create a class that inherited from DerivedClass, and you defer the implementation of one or more of DerivedClass’s methods to that class, then DerivedClass would itself have to be declared to be an abstract class. I just wanted to mention this because in most situations you will have more than a two-tiered inheritance hierarchy as I have used here in this simple example.

Let’s now take a look at the code for these two classes. Example 11.9 gives the code for AbstractClass.

Example 11.9: AbstractClass.java

image from book
 1     public abstract class AbstractClass { 2 3        public AbstractClass(){ 4            System.out.println("AbstractClass object created!"); 5        } 6 7       public abstract void printMessage(); 8 9       public abstract void setMessage(String message); 10 11    }
image from book

The important points to note here is that on line 1 the keyword abstract is used to indicate that this is an abstract class definition. The keyword is also used on lines 7 and 9 in the method declarations for printMessage() and setMessage(). Also note how both the printMessage() and setMessage() methods are terminated with a semicolon and have no body. (i.e., no curly braces!) Example 11.10 gives the code for DerivedClass.

Example 11.10: DerivedClass.java

image from book
 1     public class DerivedClass extends AbstractClass { 2       private String _message = null; 3 4       public DerivedClass(String message){ 5         _message = "Message from DerivedClass: " + message; 6         System.out.println("DerivedClass object created!"); 7       } 8 9       public DerivedClass(){ 10        this("DerivedClass message!"); 11      } 12 13      public void printMessage(){ 14        System.out.println(_message); 15      } 16 17      public void setMessage(String message){ 18         _message = "Message from DerivedClass: " + message; 19      } 20    }
image from book

DerivedClass extends AbstractClass with the extends keyword on line 1. Since this version of AbstractClass has no _message field there is no need for DerivedClass to call a base class constructor with the super keyword. DerivedClass provides an implementation for each of AbstractClass’s abstract methods. Let’s take a look now at the test driver program that will exercise these two classes.

Remember, you cannot directly instantiate an abstract class object. On line 3 a reference to an AbstractClass type object named ac1 is declared and initialized to point to a DerivedClass object. On line 4 a reference to a DerivedClass type object named dc1 is declared and initialized to point to a DerivedClass object.

Lines 6 through 8 exercises reference ac1. The printMessage() method is called on line 6 followed by a call to the setMessage() method with the String literal “I Love Java!!!” as an argument. The next time the printMessage() method is called the new message is printed to the console.

The same series of method calls is performed on the dc1 reference on lines 10 through 12. Figure 11-12 shows the results of running example 11.11.

Example 11.11: DriverApplication.java

image from book
 1     public class DriverApplication { 2       public static void main(String[] args){ 3         AbstractClass ac1 = new DerivedClass(); 4         DerivedClass dc1 = new DerivedClass(); 5         ac1.printMessage(); 6         ac1.setMessage("I Love Java!!!"); 7         ac1.printMessage(); 8         dc1.printMessage(); 9         dc1.setMessage("I hope you love Java too!!!"); 10        dc1.printMessage(); 11      } 12    }
image from book

image from book
Figure 11-12: Results of Running Example 11.11

Quick Review

An abstract method is a method that omits its body and has no implementation behavior. A class that declares one or more abstract methods must be declared to be abstract.

The primary purpose of an abstract class is to provide a specification of a set of one or more class interface methods whose implementations are expected to be found in some derived class further down the inheritance hierarchy.

Designers employ abstract classes to provide a measure of application architectural stability.




Java For Artists(c) The Art, Philosophy, and Science of Object-Oriented Programming
Java For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504052
EAN: 2147483647
Year: 2007
Pages: 452

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net