Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Object-Oriented Principles  ›  Lesson

Inheritance

Inheritance 14 minA subclass extends one superclass, inheriting its fields and methods, and its constructor must first build the parent part with super.

Inheritance

Brew Bar starts selling iced drinks. An iced latte is still a drink, with a name and a price, but it also needs an amount of ice. Copying the whole Drink class into a new file would mean fixing every future bug twice. Inheritance lets IcedDrink reuse everything Drink already has and add only what is new.

The rule

extends means "is a": the child gets everything and adds more.

public class Drink {
    protected String name;
    protected int priceCents;

    public Drink(String name, int priceCents) {
        this.name = name;
        this.priceCents = priceCents;
    }

    public String label() {
        return name + " (" + priceCents + "c)";
    }
}

public class IcedDrink extends Drink {
    private int iceGrams;

    public IcedDrink(String name, int priceCents, int iceGrams) {
        super(name, priceCents);   // build the Drink part first
        this.iceGrams = iceGrams;
    }
}

Drink is the superclass (parent) and IcedDrink the subclass (child). The child automatically has name, priceCents and label(), and adds iceGrams. Private members of the parent still exist inside the child object but cannot be named from the child's code, which is why these fields are protected.

Rules that follow:

  • super(...) builds the parent part. It must be the first statement of the child's constructor. If you leave it out, Java inserts super() with no arguments, which fails to compile here, because Drink has no such constructor.
  • Parents are built first. Constructors run from the top of the family tree down.
  • One parent only. A class can extend exactly one class. (Several interfaces are allowed, as you will see.)
  • Everything extends Object in the end, which is where methods like toString() and equals() come from.
  • A child can stand in for its parent. Drink d = new IcedDrink("Iced latte", 330, 80); is fine, because an iced drink is a drink. The reverse is not.

The shortcut

Before writing extends, say the sentence "Every ___ is a ___." If it is true, inheritance fits. If it sounds wrong, use a field instead ("an Order has drinks", so Order holds drinks rather than extending Drink).

Should Order extend Drink?Step 1 of 4
  1. Say the sentence

    "Every order is a drink." That is false: an order contains drinks.

Drills

Answer fast — trust the rule.

quick drill

With class Drink { Drink() { System.out.print("D"); } } and class IcedDrink extends Drink { IcedDrink() { System.out.print("I"); } }, what does new IcedDrink(); print?

quick drill

Which line compiles?

quick drill

What is wrong with class Mocha extends Drink, Dessert?

quick drill

The IcedDrink constructor leaves out super(name, priceCents);. What happens?

Remember

  • extends means "is a": the child gets everything and adds more.
  • Say "every ___ is a ___"; if it is false, use a field instead.
  • Traps: forgetting super(...), and trying to extend two classes.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainIs it a, or has a?

For each pair, decide whether the first should extend the second, and explain using the 'every ___ is a ___' test: (1) HotChocolate and Drink, (2) LoyaltyCard and Customer, (3) Barista and Employee. For one that should extend, describe what its constructor must do first.