Inheritance
Inheritance lets a class reuse and extend another class — but only use it for genuine “is-a” relationships.
On this page · 5 min · Beginner
On this page
- In simple words
- Learning objectives
- Explanation
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Beginner
- Updated
- Oct 30, 2026
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#
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());
}
}
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#
- SavingsAccount extends Account.
- super(opening) runs the parent constructor.
- The child adds new behaviour (interest).
- 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#
If Child is not really a Parent, use composition instead.
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
class CurrentAccount extends Account {
CurrentAccount(double opening) { super(opening); }
@Override double monthlyFee() { return 100; }
}
Mini quiz#
How many classes can a Java class directly extend?
Want more? Practise all Java MCQs · Java interview questions
My notes#
Saved in this browser.