Abstract Classes and Methods – Object-Oriented Programming

Abstract Classes and Methods – Object-Oriented Programming

5.4 Abstract Classes and Methods

The keyword abstract is used in the following contexts in Java:

  • Declaring abstract classes
  • Declaring abstract methods in classes (p. 224), in interfaces (p. 240), and in enum types (p. 294)

Abstract Classes

A concrete class is one that defines, by virtue of its public methods, a contract for services it guarantees its clients and provides the implementation for all the methods necessary to fulfill that contract. Clients can readily instantiate a concrete class and use its objects.

In certain cases, a class might want to define the contract for the services, but only provide partial implementation for its contract. Such a design decision might be necessary if the abstraction the class represents is so general that certain aspects need to be specialized by subclasses to be of practical use, but at the same time guarantee that these will be implemented by the subclasses. This design strategy can be implemented by using abstract classes. Clients cannot instantiate an abstract class, but now its concrete subclasses must be instantiated to provide the necessary objects.

The class Vehicle might be declared as an abstract class with a partially implemented contract to represent the general abstraction of a vehicle, as creating instances of the class would not make much sense. Its non-abstract subclasses, like Car or Bus, would then provide the implementation necessary to fulfill the contract of the superclass Vehicle, making the abstraction more concrete and useful.

The Java SE Platform API contains many abstract classes. The abstract class java.lang.Number is the superclass of wrapper classes that represent numeric values as objects (§8.3, p. 434). The Java Collections Framework makes heavy use of abstract classes in implementing commonly used collection data structures (§15.1, p. 783).

Declaring an abstract Class

An abstract class is declared with the modifier abstract in its class header. In Example 5.8, the class Light at (1) is declared as an abstract class. It also declares an abstract method energyCost() at (2), which has no method body and is essentially a method header (p. 224).

Click here to view code image

abstract class Light {                                     // (1) Abstract class
  //…
  // Abstract instance method:
  protected abstract double energyCost(int noOfHours)      // (2) Method header
      throws InvalidHoursException;                        // No method body
}

If a class has one or more abstract methods, it must be declared as abstract, as it is incomplete. In Example 5.8, if the abstract keyword is omitted from the header of the class Light at (1), the compiler will issue an error, as the class declares an abstract method and is therefore incomplete.

Like a normal class, an abstract class can declare class members, constructors, and initializers. The abstract class Light in Example 5.8 declares three instance fields— one non-zero argument constructor and three instance methods—in addition to the abstract method at (2).

A class that is declared absract cannot be instantiated, regardless of whether it has abstract methods or not.

Click here to view code image

Light porchLight = new Light(21, true, “Porch”);    // (5) Compile-time error! The UML class diagram for the inheritance relationship in Example 5.8 is depicted in Figure 5.3. Note that an abstract class name and an abstract method name are shown in italics to distinguish them from concrete classes and methods.