Inheritance & the extends Keyword
Inheritance & the extends Keyword
One of the most powerful ideas in object-oriented programming is inheritance — the ability for one class to automatically receive the fields and methods of another class. Instead of writing the same code in five places, you write it once in a superclass and let every related subclass reuse it for free.
The Is-A Relationship
Before you touch the keyboard, ask one question: does my new class represent a more specific version of an existing class? If a Dog is a kind of Animal, that is an is-a relationship, and inheritance is the right tool. If a Car has an Engine, that is a has-a relationship — use composition instead (we cover that in a later lesson).
Dogis aAnimal— good fit for inheritance.SavingsAccountis aBankAccount— good fit.Carhas anEngine— use composition, not inheritance.
extends.
Superclass and Subclass
In Java the class being inherited from is called the superclass (also called parent class or base class). The class that inherits is called the subclass (also child class or derived class). You create the relationship with the extends keyword.
Dog extends Animal, so every Dog object automatically has name, age, eat(), and sleep() — without a single extra line inside the Dog class. The Dog class only defines what makes a dog different from a generic animal: the breed field and the bark() method.
What Gets Inherited
A subclass inherits everything that the superclass declares with public or protected access. Concretely:
- Instance fields —
nameandagein the example above. - Instance methods —
eat()andsleep(). - Static (class) members — also inherited, though static methods are not polymorphically dispatched.
What is not inherited:
- Private members — fields and methods marked
privateexist in the superclass object but are not directly accessible from the subclass. - Constructors — constructors are never inherited; the subclass must define its own (or the compiler adds a default one). You can call the superclass constructor using
super(), which we cover in the next lesson.
Dog, the underlying Animal portion of the object is fully initialised. You just cannot read or write private superclass fields directly; use protected or public getters instead.
A Runnable Example
Let us put it all together and run it:
Output:
Dog did not define eat() or sleep(), yet we call them on a Dog instance without any issues. That is inheritance at work.
Extending a Chain: Multi-Level Inheritance
Java allows a subclass to itself be extended. You can have Animal → Dog → GuideDog. Each level inherits everything from all levels above it.
A GuideDog object now has name, age, breed, eat(), sleep(), bark(), and guide() — all accumulated across three levels.
Why Inheritance Matters
Inheritance gives you three concrete benefits:
- Code reuse — write shared behaviour once in the superclass instead of duplicating it across many classes.
- Extensibility — add a new kind of
Animalby creating a new subclass; the shared code does not change. - Polymorphism — a variable of type
Animalcan hold aDog, aCat, or any other subclass. This is the topic of Lesson 5 and it unlocks some of Java's most powerful patterns.
Summary
Use the extends keyword when a new class has a genuine is-a relationship with an existing class. The subclass inherits all public and protected members of the superclass and adds its own specialisations on top. Constructors and private members are not inherited. In the next lesson we will look at super and how to call the superclass constructor properly.