TExcelTAD
Home › Java › Java Inheritance

Java Inheritance

Inheritance lets you create a new class based on an existing one. The new class — the subclass (or child) — automatically gets the fields and methods of the superclass (or parent), and can add its own or change inherited behaviour. It models an is-a relationship: a Car is a Vehicle, a Manager is an Employee.

extends

java · try it yourself
class Vehicle {
    String brand;
    int wheels = 4;

    void honk() {
        System.out.println(brand + ": beep beep!");
    }
}

class Bicycle extends Vehicle {
    boolean hasBell = true;

    void ring() {
        System.out.println(brand + ": ring ring!");
    }
}

class Main {
    public static void main(String[] args) {
        Bicycle bike = new Bicycle();
        bike.brand = "Brompton";   // inherited field
        bike.wheels = 2;           // inherited field
        bike.honk();               // inherited method
        bike.ring();               // its own method
        System.out.println(bike.wheels + " wheels, bell: " + bike.hasBell);
    }
}
Brompton: beep beep!
Brompton: ring ring!
2 wheels, bell: true

Java supports single inheritance for classes: a class can extend only one parent. (A class can implement many interfaces, covered later.)

Constructors and super(...)

Constructors are not inherited. A subclass constructor must first initialise the parent part of the object by calling a parent constructor with super(...) as its first statement. If you don't write one, Java inserts super() automatically — which only works if the parent has a no-arg constructor.

java · try it yourself
class Employee {
    protected String name;
    protected double salary;

    Employee(String name, double salary) {
        this.name = name;
        this.salary = salary;
    }

    String describe() {
        return name + " earns " + salary;
    }
}

class Manager extends Employee {
    private int teamSize;

    Manager(String name, double salary, int teamSize) {
        super(name, salary);          // run Employee's constructor first
        this.teamSize = teamSize;
    }

    int getTeamSize() {
        return teamSize;
    }
}

class Main {
    public static void main(String[] args) {
        Manager m = new Manager("Priya", 85000, 6);
        System.out.println(m.describe());
        System.out.println("Team size: " + m.getTeamSize());
    }
}
Priya earns 85000.0
Team size: 6

protected fields are visible to subclasses (and the same package) but hidden from unrelated classes.

Overriding methods

A subclass can override an inherited method by declaring one with the same name and parameters. Use @Override so the compiler checks that you really are overriding something. Inside the override, super.method() calls the parent version:

java · try it yourself
class Employee {
    protected String name;
    protected double salary;

    Employee(String name, double salary) {
        this.name = name;
        this.salary = salary;
    }

    double yearlyBonus() {
        return salary * 0.05;
    }

    String describe() {
        return name + " (bonus " + yearlyBonus() + ")";
    }
}

class Manager extends Employee {
    Manager(String name, double salary) {
        super(name, salary);
    }

    @Override
    double yearlyBonus() {
        return super.yearlyBonus() + 2000;   // parent bonus plus extra
    }

    @Override
    String describe() {
        return "Manager " + super.describe();
    }
}

class Main {
    public static void main(String[] args) {
        Employee e = new Employee("Tom", 40000);
        Manager m = new Manager("Priya", 80000);
        System.out.println(e.describe());
        System.out.println(m.describe());
    }
}
Tom (bonus 2000.0)
Manager Priya (bonus 6000.0)

Notice that describe() in Employee calls yearlyBonus() — and for a Manager object, the overridden version runs. This is the foundation of polymorphism (next chapter).

Rules for overriding:

Rule Detail
Same signature same name and parameter types
Return type same, or a subtype
Access can't be more restrictive (a public method stays public)
Can't override final, static or private methods

The Object class

Every class that doesn't extend anything implicitly extends java.lang.Object. That's where toString(), equals() and hashCode() come from. Overriding toString and equals is very common:

java · try it yourself
class Point {
    final int x, y;

    Point(int x, int y) {
        this.x = x;
        this.y = y;
    }

    @Override
    public String toString() {
        return "(" + x + ", " + y + ")";
    }

    @Override
    public boolean equals(Object other) {
        if (!(other instanceof Point p)) return false;
        return x == p.x && y == p.y;
    }

    @Override
    public int hashCode() {
        return 31 * x + y;
    }
}

class Main {
    public static void main(String[] args) {
        Point a = new Point(2, 3);
        Point b = new Point(2, 3);
        System.out.println(a);
        System.out.println("a == b: " + (a == b));
        System.out.println("a.equals(b): " + a.equals(b));
        System.out.println(a instanceof Object);
    }
}
(2, 3)
a == b: false
a.equals(b): true
true

Note: When you override equals, always override hashCode too, so equal objects behave correctly in HashMap and HashSet.

Preventing inheritance

Mark a class final to forbid subclasses, or a method final to forbid overriding it. Java 17 also added sealed classes, which allow only a fixed list of subclasses: sealed class Shape permits Circle, Square { }.

Tip: Use inheritance only for true is-a relationships. If one class merely uses another (a Car has an Engine), give it a field instead — this is called composition and is usually more flexible.

Test yourself

Which keyword makes a class inherit from another?

  1. implements
  2. inherits
  3. extends
  4. super

How many classes can a Java class directly extend?

  1. None
  2. One
  3. Two
  4. Unlimited

What does super.describe() do inside an overriding describe()?

  1. Calls the method recursively
  2. Calls the parent class's version
  3. Calls the constructor
  4. Nothing - it is a syntax error