Menu

Inheritance in Java

Inheritance in Java

Imagine writing separate Car, Bike, and Truck classes, each repeating fields like speed and brand, and methods like accelerate() and brake(), almost word for word. Inheritance exists precisely to eliminate this repetition, by letting one class acquire the fields and methods of another.

What is Inheritance?

Inheritance lets a class (the subclass) automatically gain the fields and methods of another class (the superclass), then add its own specific fields and methods on top, or change the behaviour it inherited. It represents an 'is-a' relationship: a Dog is an Animal, a SavingsAccount is a BankAccount.

This is the single biggest lever OOP gives you for code reuse. Write the shared behaviour once in a general class, and every more specific class built on top of it gets that behaviour for free.

The extends Keyword

In Java, a class inherits from another using the extends keyword, written right after the subclass's name.

class Animal {

String name;

void eat() {

System.out.println(name + " is eating.");

}

}

class Dog extends Animal {

void bark() {

System.out.println(name + " says Woof!");

}

}

public class Main {

public static void main(String[] args) {

Dog myDog = new Dog();

myDog.name = "Rex";    // inherited field from Animal

myDog.eat();          // inherited method from Animal

myDog.bark();         // Dog's own method

}

}

Dog never declared a name field or an eat() method itself, yet both are available on myDog, because Dog extends Animal. Java compiles to support exactly one direct superclass per class, but that superclass can itself extend another, forming a chain.

Superclass and Subclass

Term

Also Called

Role

Superclass

Parent class, base class

The general class being inherited from (e.g. Animal)

Subclass

Child class, derived class

The more specific class doing the inheriting (e.g. Dog)

A subclass can access the public and protected members of its superclass directly. To call a superclass's constructor explicitly, or to reach a method that the subclass has overridden, you use the super keyword.

class Animal {

String name;

Animal(String n) {

name = n;

}

}

class Dog extends Animal {

Dog(String n) {

super(n);   // calls Animal's constructor

}

}

Types of Inheritance in Java

Type

Description

Supported Directly with Classes in Java?

Single

One subclass inherits from exactly one superclass

Yes

Multilevel

A class is derived from a class that is itself derived from another (a chain)

Yes

Hierarchical

Multiple subclasses inherit from the same single superclass

Yes

Multiple

A class inherits from more than one superclass at once

No — only via interfaces

Hybrid

A mix of two or more inheritance types

Only achievable using interfaces

Java deliberately does not allow a class to extend more than one other class. This avoids the 'diamond problem', the ambiguity that arises when two parent classes define a method with the same signature and the compiler cannot decide which version a subclass should inherit. Multiple inheritance of behaviour is still possible in Java, but only through interfaces, which you will meet in lesson 7.

Did You Know?

Every class in Java implicitly inherits from the Object class if it does not explicitly extend anything else. This is why every Java object automatically has methods like toString() and equals() available, even if you never wrote them yourself.