GNGiseldo Neo and Alana NeoBooks
Introduction to Programming in Java13 / 25

Inheritance

Specializing behavior without creating fragile hierarchies

12.1 Learning Objectives

By the end of this chapter, you should be able to:

  • recognize a genuine specialization relationship;
  • initialize the superclass with super;
  • override methods with @Override;
  • use abstract classes;
  • justify when composition is preferable.

12.2 The “Is-A” Relationship

A simple hierarchy based on an is-a relationship.

Figure 12.1. A simple hierarchy based on an is-a relationship.

Inheritance represents an “is-a” relationship. Before using extends, test the statement “every X is a Y” and confirm that the subclass can uphold the superclass contract.

abstract class BotMessage {
    private final String category;

    protected BotMessage(String category) {
        this.category = category;
    }

    public String getCategory() {
        return category;
    }

    public abstract String text();
}

final class Greeting extends BotMessage {
    Greeting() {
        super("greeting");
    }

    @Override
    public String text() {
        return "Hello! Ready to study Java?";
    }
}

final class Tip extends BotMessage {
    Tip() {
        super("tip");
    }

    @Override
    public String text() {
        return "Keep methods small and testable.";
    }
}

public class Chapter12Inheritance {
    public static void main(String[] args) {
        BotMessage message = new Tip();
        System.out.println(message.getCategory());
        System.out.println(message.text());
    }
}

The reference has type BotMessage, but the method executed belongs to Tip. This behavior prepares the ground for studying polymorphism.

12.3 Constructors and super

Constructors are not inherited. Superclass initialization runs before the subclass constructor body. If the superclass does not have a no-argument constructor, the subclass must call super(...).

private members remain part of the object, but can only be accessed through the superclass’s own methods. Avoid turning fields into protected merely for convenience; prefer protected or public operations that maintain invariants.

12.4 Method Overriding

@Override lets the compiler verify your intention to override a method. The signature must be compatible, and the subclass must not weaken the expected contract.

Overloading and overriding are different:

  • overloading: same name, different parameters, resolved at compile time;
  • overriding: same signature in a subclass, resolved at runtime.

12.5 Abstract Classes and final

An abstract class represents an incomplete concept and can combine state, concrete methods, and abstract methods. A final class prevents inheritance when extension was not planned.

Avoid deep hierarchies. Two or three levels already require care; deeper chains make changes and testing much harder.

12.6 When to Prefer Composition

  • Car has an engine: composition.
  • Chatbot has a history: composition.
  • Tip is a bot message: inheritance can be appropriate.

If the goal is merely to reuse a few lines of code, inheritance is probably the wrong tool.

The complete example is in examples/src/Chapter12Inheritance.java.

12.7 Verifiable Practice

Create a new subclass Warning that returns a caution message. Then answer:

  1. which state belongs to the superclass?
  2. which behavior was overridden?
  3. does replacing Tip with Warning keep the program valid?
  4. would it be better to model this case with composition?

Inheritance is not code copying

Use inheritance to represent behavioral substitution, not as an automatic code-reuse mechanism.