Java · Object-Oriented Java

Inheritance

Inheritance lets a class reuse and extend another class — but only use it for genuine “is-a” relationships.

Beginner 5 min Lesson 13 of 31Examples target Java 21 LTS (they also run on Java 25).
Before you start:Encapsulation
0/31 in Java
On this page · 5 min · Beginner

In simple words#

A child inherits family traits from parents and adds their own. A SavingsAccount inherits everything an Account has and adds interest.

extends
class Savings extends Account.
super
Refers to the parent class (constructor or methods).
@Override
Marks a method that replaces the parent’s.

Learning objectives#

  • Extend a class
  • Call the parent constructor with super
  • Override methods safely
  • Know when to prefer composition

Explanation#

class Child extends Parent inherits accessible fields and methods. Java allows one parent class.

The child constructor must call a parent constructor (super(...)) first.

@Override tells the compiler you intend to replace a parent method — it catches typos.

Every class ultimately extends java.lang.Object (toString, equals, hashCode). Use inheritance only when Child is truly a Parent.

Code example: Savings account adds interest#

JavaSavings account adds interest Illustrative
class Account {
    protected double balance;
    Account(double opening) { balance = opening; }
    double monthlyFee() { return 50; }
    @Override public String toString() { return getClass().getSimpleName() + " ₹" + balance; }
}


class SavingsAccount extends Account {
    private final double rate;
    SavingsAccount(double opening, double rate) {
        super(opening);              // parent constructor first
        this.rate = rate;
    }
    void addMonthlyInterest() { balance += balance * rate / 12; }
    @Override double monthlyFee() { return balance > 10000 ? 0 : super.monthlyFee(); }
}


public class Bank {
    public static void main(String[] args) {
        SavingsAccount s = new SavingsAccount(24000, 0.06);
        s.addMonthlyInterest();
        System.out.println(s + ", fee ₹" + s.monthlyFee());
    }
}
Expected output
SavingsAccount ₹24120.0, fee ₹0.0

This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.

Step-by-step explanation#

  1. SavingsAccount extends Account.
  2. super(opening) runs the parent constructor.
  3. The child adds new behaviour (interest).
  4. monthlyFee is overridden and can reuse super.monthlyFee().

Real-world use case#

Payment types (UPI, Card, Wallet) share common fields in a base class and override how they process payments.

Common mistakes#

Inheritance for code reuse only

If Child is not really a Parent, use composition instead.

Forgetting @Override

A typo creates a new method silently. @Override makes it a compile error.

Best practices#

  • Use for true “is-a” relationships
  • Always @Override
  • Keep hierarchies shallow
  • Prefer composition for flexibility

Remember this#

extends = inherits from one parent.

super(...) first in the constructor.

@Override every time.

Practice exercise#

Your task: Create a CurrentAccount that overrides monthlyFee() to return 100.

Show solution
Java Illustrative
class CurrentAccount extends Account {
    CurrentAccount(double opening) { super(opening); }
    @Override double monthlyFee() { return 100; }
}

Mini quiz#

Inheritance quizMultiple choice Question 1 of 1

How many classes can a Java class directly extend?

Score so far: 0/0

Want more? Practise all Java MCQs · Java interview questions

My notes#

Saved in this browser.

Progress is saved in this browser. Sign in to sync.