Skip to main content
๐Ÿ“œ WAYPOINT LESSON

extends and super

โญ beginnerโณ 14 min read๐Ÿ“ Lesson 49 of 256

Subclasses, private access across the hierarchy, implicit vs explicit super.

Inheritance models is-a relationships. A Dog is-a Pet: it gets every non-private member of Pet for free and adds its own.

public class Pet {
    private String name;

    public Pet(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public String speak() {
        return "...";
    }
}

public class Dog extends Pet {
    private boolean fetchTrained;

    public Dog(String name, boolean fetchTrained) {
        super(name);               // MUST be first: build the Pet part
        this.fetchTrained = fetchTrained;
    }

    public boolean isFetchTrained() {
        return fetchTrained;
    }
}

Reading rules:

  • private fields exist inside Dog objects but are not directly accessible from Dog code โ€” go through the superclass's public accessors (getName()).
  • super(name) calls the superclass constructor. If you omit it, Java inserts an implicit super() โ€” which only compiles if the superclass has a no-arg constructor. This is the #1 inheritance compile error.

Revised-framework note: inheritance appears in Unit 3 (10โ€“18% of the exam) with modest depth โ€” extends, super, one level of overriding. Hierarchy depth beyond one level, protected, abstract classes, and interfaces are not in this course.