3. Subclasses and Inheritance

The derived class is called a subclass. The class from which its derived is called the superclass.

Figure 4.1. Subclass

Subclass

In Java all classes must be derived from some class. The top-most class, the class from which all other classes are derived, is the Object class defined in java.lang:

Figure 4.2. Inheritance

Inheritance

3.1. Definition

A subclass is a class that derives from another class. It subclass inherits state and behavior in the form of variables and methods from all of its ancestors. It can just use the items inherited from its superclass as is, or the subclass can modify or override it. So, as you drop down in the hierarchy, the classes become more and more specialized.

Audio in Portuguese

To declare a subclass you would write:

class SubClass extends SuperClass { . . . }
[Caution]Caution

A Java class can have only one direct superclass. Java does not support multiple inheritance.

3.2.1. Seen Hidden Member Variables

Member variables defined in the subclass hide member variables of the same name in the superclass. Consider this superclass and subclass pair:

class Super {
    Number aNumber;
}
class Sub extends Super {
    Float aNumber;
}
super.aNumber

A subclass can either completely override the implementation for an inherited method or the subclass can enhance the method by adding functionality to it.

  • Replacing a Superclass's Method Implementation

  • Adding to a Superclass's Method Implementation

  • Methods a Subclass Cannot Override

    class ChessAlgorithm {
       . . .
       final void nextMove(ChessPiece piece) {...}
       static void mate(ChessPiece piece) {...}
       . . .
    }
  • Methods a Subclass Must Override

    abstract class GraphicObject {
       . . .
       abstract void draw();
       . . .
    }

Audio in Portuguese

3.4.1. Final Classes

You can declare that your class is final; that is, that your class cannot be subclassed. There are (at least) two reasons why you might want to do this: security reasons and design reasons.

3.4.2. Final Methods

You can use the final keyword in a method declaration to indicate to the compiler that the method cannot be overridden by subclasses.

3.5.1. Abstract Classes

Classes, which implement abstract concepts and should not be instantiated, are called abstract classes. An abstract class is a class that can only be subclassed - it cannot be instantiated.

3.5.2. Abstract Methods

An abstract class may contain abstract methods, that is, methods with no implementation. In this way, an abstract class can define a complete programming interface thereby providing its subclasses with the method declarations for all of the methods necessary to implement that programming interface.

3.5.3. Architectural advantage

Abstract classes provide a way to represent objects at a conceptual generic level.

In a graphic library, you can create an abstract class to represent a generic graphic object and a method for drawing it. This method is them only implemented on the derived classes.

Figure 4.3. Graphic Classes

Graphic Classes

Audio in Portuguese