Abstract Classes


If you want all subclasses to share a method's signature and behavior, you can just create a concrete superclass with that method signature and implementation. What if you have a second method whose signature will be shared by all subclasses, but the behavior differs among the subclasses? In this case, you declare an abstract class and include the first method as you normally would, but then add the second method declared as abstract. The first method is simply inherited as expected. The second method that is declared abstract must be implemented in each subclass. Abstract classes are designed for those classes in which some methods are simply inherited and others are overridden. This is different from an interface, in which all its methods must be implemented in a subclass. In contrast, only the methods declared as abstract in an abstract class need to be implemented in a concrete class. The other methods in an abstract class, those not declared abstract themselves , are simply inherited.

graphics/tip_icon.gif

If you know the behavior that all subclasses will share, place it in methods in an abstract class, without declaring these methods as abstract. If you know a method signature that all subclasses will share but the behavior will be unique to each sub class, declare this method in an interface or declare it as abstract without a body in an abstract class.


To declare a class as an abstract class, begin the class declaration with the abstract keyword, like so:

 abstract class Number {     //class methods } 

If you take a concrete class and add the abstract keyword to the class declaration, the only change will be that this class can no longer be instantiated as an object. To make use of it, you have to use it as a superclass. To subclass this abstract class, you extend it in a concrete class. To inherit methods from an abstract class, you must extend that abstract class. This makes sense because you inherit the implemented methods in the abstract class, but override the abstract methods in the subclass. Listing 9.1 shows an example of an abstract class (see Listing 9.2 for its subclass that extends it).

Listing 9.1 An Example of an Abstract Class
 abstract class MyAbstractSuperclass {   protected MyAbstractSuperclass()   {       System.out.println("MyAbstractClass");   }   public void inherited_method()   {     System.out.println("inherited_method");     must_implement_method();   }   public abstract void must_implement_method(); } 

In Listing 9.1, the class is declared as abstract , which tells the compiler that it cannot be instantiated itself. You will get an error if you try. There is nothing special about the constructor. The inherited_method() is a normal method; it is declared and implemented here as it would be in any concrete class. When this class is extended, the subclass inherits the inherited_method() method as expected. So far, this class is the same as any other except for the abstract keyword in the class declaration.

The must_implement_method() method is what sets this class apart from concrete classes. It has no body. It looks like a method declaration in an interface. Actually, that is how the compiler processes the must_implement_method() method; it must be implemented in a concrete subclass. As you can see, an abstract class is a specialized interface because some abstract methods possess inherited behavior when implemented. The remaining methods are declared as abstract, which is necessary to be implemented in a concrete class.

Listing 9.2 is an example of a class that extends the abstract class in Listing 9.1. Notice which class is inherited and which one is implemented to make the class in Listing 9.2 a concrete class.

Listing 9.2 An Example of a Subclass Extending an Abstract Class
 class MySubclass extends MyAbstractSuperclass {   protected MySubclass()   {     System.out.println("MySubclass");   }   public void must_implement_method()   {     System.out.println("must_implement_method");   }   public static void main(String args[])   {     System.out.println("main");     MySubclass mySubclass = new MySubclass();     mySubclass.inherited_method();     mySubclass.must_implement_method();   } } /* returns: main MyAbstractClass MySubclass inherited_method must_implement_method must_implement_method */ 

Remember that not all methods inside an abstract class are overridden in its subclass. The inherited_method() of the abstract class in Listing 9.1 is simply inherited, and the must_implement_method() method is implemented by the concrete class in Listing 9.2.

graphics/tip_icon.gif

Behaviors that are common to all subclasses should be implemented as concrete methods within the parent class (which can be abstract). If you are not sure that any method in a group of methods will have behavior common among all subclasses, then place these method signatures in an interface class. Note that this could be covered by abstract methods within an abstract superclass instead of via interface methods, and note that the use of an interface is preferred.


Abstract classes are a powerful way to standardize behavior in an application. If you have a behavior that you want to be exactly the same throughout the application (for instance, inherited in concrete subclasses), implement it in an abstract class. Where should you standardize method signatures within an application? Some developers prefer to place them in interfaces, and others place them in abstract classes. I recommend the following guide to help you decide where to place a given method:

  • Interface ” Standardize the signature. The behavior will be implementation specific.

  • Abstract class ” Standardize the behavior. The behavior will be the same throughout the application, so define it in an abstract class for all subclasses that will inherit it.

When choosing whether to declare a method in an interface or abstract class, you must consider that Java classes support only single inheritance: A class can extend at most only one superclass. It can, however, implement multiple interfaces. If a base class needs to inherit (or implement) multiple API contracts, these contracts should be defined in an interface, not an abstract class.

The ideal structure has a list of methods that you want to standardize in an interface as signatures only ”these methods have no body. Implement the interface in an abstract class where you add the behavior to those methods; you can use a few or all of the methods in the implemented interface that you standardize for the application. Last, write a concrete class that is a subclass of the abstract class, making sure to override all methods in the interface that are not implemented in the abstract class.

graphics/note_icon.gif

An abstract method is a method with no implementation. An abstract class provides its subclasses with method declarations for all the methods required to be implemented. A concrete subclass must implement all abstract methods in the abstract superclass. Note that you can have an abstract class inherit from an abstract class.




JavaT 2 Developer Exam CramT 2 (Exam CX-310-252A and CX-310-027)
JavaT 2 Developer Exam CramT 2 (Exam CX-310-252A and CX-310-027)
ISBN: N/A
EAN: N/A
Year: 2003
Pages: 187

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