Learning outcome: Build inheritance hierarchies, distinguish common inheritance forms, and use abstract classes to represent incomplete general concepts.
Inheritance lets one class reuse and specialise members of another class. It models an is-a relationship: a Dog is an Animal, and a SavingsAccount is an Account. Private superclass state is still part of each subclass object, but subclass code cannot access those private members directly; it uses accessible superclass behaviour instead.
Inheritance Terminology
| Term | Meaning |
|---|---|
| Superclass, parent, or base class | The class whose members are inherited. |
| Subclass, child, or derived class | The class that extends another class. |
extends |
Keyword used for class inheritance. |
| IS-A relationship | The subtype relationship created by inheritance. |
class Animal {
protected String name;
void eat() {
System.out.println(name + " is eating");
}
}
class Dog extends Animal {
void bark() {
System.out.println(name + " is barking");
}
}
Dog inherits the accessible members of Animal and adds its own behaviour.
Single Inheritance
One child class extends one parent class.
In simple terms: extends says is-a, not has-a. A Car is a Vehicle, so it inherits; a Car has an Engine, so it holds one as a field instead.
class Vehicle {
void move() {
System.out.println("Vehicle moves");
}
}
class Bicycle extends Vehicle {
void ringBell() {
System.out.println("Ring ring");
}
}
Multilevel Inheritance
A class extends a class that already extends another class.
class LivingThing {
void grow() { System.out.println("Growing"); }
}
class Animal extends LivingThing {
void eat() { System.out.println("Eating"); }
}
class Cat extends Animal {
void meow() { System.out.println("Meow"); }
}
A Cat can use accessible behaviour declared by both Animal and LivingThing.
Hierarchical Inheritance
Several child classes extend the same parent.
class Shape {
void describe() {
System.out.println("A geometric shape");
}
}
class Circle extends Shape { }
class Rectangle extends Shape { }
Why Classes Do Not Support Multiple Inheritance
Java does not allow a class to extend more than one class.
In simple terms: If two parents each define the same method, the child has no principled way to choose between them. Java avoids the argument entirely: one parent class, and as many interfaces as you like.
// Invalid Java
// class SmartDevice extends Phone, Camera { }
Multiple class inheritance can create ambiguous state and implementation paths. Java instead allows a class to implement multiple interfaces. Interfaces may provide default methods; when inherited defaults conflict, the class must override the method and resolve the conflict explicitly. This does not add multiple inheritance of class instance state.
Constructors and super
Constructors are not inherited. A child constructor can call a parent constructor with super(...). That call must be the first statement in the child constructor.
In simple terms: A child cannot furnish a house whose foundation is unbuilt. super(...) lays the parent’s part first, which is exactly why it must be the very first statement.
class Employee {
private final String name;
Employee(String name) {
this.name = name;
}
String getName() {
return name;
}
}
class Developer extends Employee {
Developer(String name) {
super(name);
}
}
Abstraction
Abstraction exposes essential behaviour while hiding implementation details. Java supports abstraction through abstract classes and interfaces.
In simple terms: An abstract class is a form with some fields already filled in and the rest left blank for whoever completes it. The blank form itself cannot be submitted — only a completed copy.
An abstract class:
- is declared with
abstract; - cannot be instantiated directly;
- may contain abstract methods and concrete methods;
- may contain fields and constructors.
abstract class Shape {
private final String color;
protected Shape(String color) {
this.color = color;
}
public String getColor() {
return color;
}
public abstract double area();
}
class Circle extends Shape {
private final double radius;
Circle(String color, double radius) {
super(color);
this.radius = radius;
}
@Override
public double area() {
return Math.PI * radius * radius;
}
}
The abstract method declares a contract. Each concrete child supplies the missing implementation.
Abstract Class Rules
| Rule | Explanation |
|---|---|
| Abstract classes cannot be instantiated | Create an object of a concrete subclass instead. |
| A class with an abstract method must be abstract | An incomplete class must be marked explicitly. |
| A concrete subclass must implement inherited abstract methods | Otherwise, the subclass must also be abstract. |
| Abstract methods have no body | The method declaration ends with ;. |
| Constructors are allowed | They initialise the inherited part of child objects. |
Inheritance vs Composition
Use inheritance for a genuine is-a relationship. Use composition for a has-a relationship.
In simple terms: Test whether the sentence “an X is a Y” is actually true. A Stack is not an ArrayList; it uses one. Getting this backwards leaks methods you never meant to offer.
| Relationship | Example | Preferred mechanism |
|---|---|---|
| A dog is an animal | Dog extends Animal |
Inheritance |
| A car has an engine | Car contains an Engine field |
Composition |
Common Mistakes
| Mistake | Correction |
|---|---|
| Using inheritance only to reuse a few lines | Confirm that the child truly is a specialised parent. |
| Trying to instantiate an abstract class | Instantiate a concrete subclass. |
| Assuming private superclass members are directly accessible | The private state remains encapsulated in the superclass; use its accessible methods. |
Forgetting super(...) when the parent has no no-argument constructor |
Call the required parent constructor explicitly. |
Practice
- Create a
Personparent and aStudentchild. - Extend the hierarchy with
GraduateStudentto demonstrate multilevel inheritance. - Create an abstract
Paymentclass with an abstractpay(double amount)method. - Decide whether
LibraryandBookshould use inheritance or composition.
Quick Check
1. Which keyword creates class inheritance?
The extends keyword makes one class a subclass of another.
2. Can one Java class extend two classes?
No. Java classes support single inheritance. Multiple capabilities can be represented with interfaces.
3. Can an abstract class have a constructor?
Yes. Its constructor initialises the parent portion of objects created from concrete subclasses.
Summary
Inheritance models an is-a relationship and supports single, multilevel, and hierarchical class structures. Abstract classes capture shared state and implemented behaviour while leaving selected operations for concrete subclasses. Use inheritance deliberately and prefer composition for has-a relationships.