Witscale Test Center

3.3 Class modifiers > 3.3.1 The abstract modifier


3.3.1 The abstract modifier

One simple way to explain inheritance is as a technique by which you place the most common behavior in superclass and delegate implementation of special behavior to subclasses. Subclasses inherit the common behavior placed in their superclass for code reuse. However superclasses can play bigger role than just to keep the common behavior inside them. They can also define the special behavior but provide no implementation (we will soon see why). An abstract class is such a superclass that dictates the specialized behavior that its subclasses must implement. You can declare an abstract class by applying the abstract modifier to the class declaration. You can then define abstract methods defining the special behavior in it. Since the abstract class has partial implementation, you cannot instantiated it.

Why abstract classes?

If an abstract class is not fully implemented and cannot be instantiated, why would you write an abstract class? Let us discuss some scenarios where you may need an abstract class:

To model an abstract concept

Sometimes, a class represents an abstract concept and as such, it does not make sense for objects of that type to exist. Say for instance, abstract concepts like food or animal in the real world. We don’t see instances of food or animal in real world. What we see instead are the instances of subclasses of food such as bread, rice and so on. Food is an abstract representation of things we can eat. You may want to model an abstract concept like food without letting any instance creation of that concept. You can do so by defining an abstract class to represent that concept.

 

To record all expectations

An animal is yet another abstract concept. An animal can be a Dog, Bear, Kangaroo or Squirrel. Each of these animals moves from one place to another, but each does so differently. Let us declare an abstract class Animal and a concrete class, Kangaroo, that extends it. As each animal can move, the Animal class may declare a move() method. But since dogs, squirrels, kangaroos move differently, each of these subclass is likely to have separate implementation for move() method. Therefore it could be futile to implement the

move() method in Animal class. So an abstract method move() is defined which does not have any implementation. Listing 3.9 illustrates how you can declare an abstract class and its concrete subclass.

 


 

 


You may wonder why to define an abstract method that does not seem to have any direct use in the declaring class. But when you do so, say for instance, when you declare the move() method as abstract in Animal class, it mandates all its subclasses to implement it. This method is declared in Animal just to record this expectation. Thus, an abstract class can define abstract methods for the abstract behavior that it expects all of its subclasses to have. 

 

You can use an abstract class to define a programming interface for classes in the form of abstract methods. Because it mandates all its subclasses to provide an implementation for all the abstract methods, the subclasses automatically comply with this common interface.

 

To define common behavior

Abstract class’s purpose is similar to a Java interface as both describe the common behavior. But one of the facts that differentiate the two is an interface has no implementation whereas the abstract class can provide implementation for some common behavior. This common behavior is defined in concrete methods of the abstract class. It is sharable among the subclasses as they inherit the concrete methods. In our example, the abstract class Animal may have a concrete method breathe(), since different animals may have a common breathing behavior:

 

The advantage you get by defining breathe() in Animal is that you avoid redundancy of writing same method in each subclass.  If you decide to change breathe(), you need to make changes only at one place. Since all subclasses inherit this method, they will automatically inherit the modified method.

 

 

Declaration rules for abstract Class

There are certain rules you need to follow when you declare abstract classes and their subclasses:

§         If a Java class has one or more abstract methods, it must be defined as abstract.

§         Abstract class can also have concrete methods besides the abstract methods.

§         An abstract class cannot be instantiated. If anyone tries to create an instance of an abstract class, the compiler prevents it.

         The subclass of an abstract class must provide an implementation for all the abstract methods, unless the subclass further defers the implementation to its subclasses by declaring itself as abstract. For example, if abstract class Animal has a subclass Reptile that does not implement abstract move method, you need to declare Reptile as abstract as well. Thus if you further decide to subclass Reptile, that subclass will have the responsibility to implement that abstract method (or declare itself as abstract of course).

         An abstract class can be declared without any abstract methods in it. In that case, the only restriction it has is that it cannot be instantiated.

 

We have discussed why and when to declare abstract classes in detail. Though it does not hurt, SCJP generally expects you to understand only the above declaration rules about abstract classes, rather than the design situations where you should (or should not) define an abstract class.