๐ฏ What you will learn
Copying code between classes is the enemy of maintainability. Inheritance lets you define shared behaviour once in a parent class and specialise it in child classes. By the end of this lesson you will understand:
- The
extendskeyword and what a subclass automatically inherits - Calling the parent constructor with
super(args)โ and why it must come first - Calling a parent method with
super.method()inside an override - The
@Overrideannotation and why you should always use it - The
protectedaccess modifier for subclass-visible fields - Multi-level class hierarchies and when to use them
๐งฌ What is inheritance?
Inheritance lets a subclass reuse and extend an existing class. class Child extends Parent means Child automatically has all Parent fields and methods. super(args) calls the parent constructor; @Override replaces a parent method. Java supports single inheritance (one parent only) but unlimited depth.
Key rule: super(args) must be the very first statement in a child constructor. The JVM requires the parent portion of the object to be fully initialised before the child's code runs.
๐ฆ Real-world scenario
SouthBank India has three loan types: generic term loans, and specialised home loans that add property city tracking. Arjun models this as a three-level class hierarchy: BankAccount base โ SavingsAccount child โ then a separate TermLoan โ HomeLoan chain. He uses super() to wire constructors and @Override to customise behaviour at each level.
๐ ๏ธ Basic inheritance โ extends and super()
A SavingsAccount is a specialised BaseAccount. It inherits accountNo, owner, balance, and the deposit() method for free โ it only needs to add the interest rate and override summary().
๐ Calling super.method() inside an override
When you override a method but still want the parent's behaviour as part of it, call super.method() explicitly. Here CurrentAccount overrides both describe() and transfer(), extending the parent version rather than replacing it entirely.
๐ Three-level hierarchy โ LoanProduct โ TermLoan โ HomeLoan
Multi-level chains are valid โ each level extends the one above. HomeLoan calls super() to TermLoan, which calls super() to LoanProduct. The describe() chain builds the string incrementally with each super.describe() call.
๐ Quick reference โ inheritance keywords
| Keyword | Purpose |
|---|---|
extends | Declares parent class |
super(args) | Calls parent constructor (must be first) |
super.method() | Calls parent's version of an overridden method |
@Override | Annotation that confirms method overrides parent |
protected | Visible to subclasses and same package |
final class | Cannot be extended |
final method | Cannot be overridden |
๐ค Interview Q&A
The JVM requires the parent's portion of the object to be fully initialised before the child's constructor body runs. If super() could appear later, child code could access inherited fields before they were set up, leading to undefined state. The compiler enforces this โ placing any statement before super() is a compile error.
Overloading: same class, same method name, different parameter types โ resolved at compile time. Overriding: subclass replaces a parent method with the same signature โ resolved at runtime based on actual object type. Overloading is static (compile-time) polymorphism; overriding is dynamic (runtime) polymorphism.
@Override is a compile-time annotation that instructs the compiler to verify the method actually overrides a parent method. If you misspell the name or use the wrong parameters, the compiler errors instead of silently creating a new method. Without it, bugs from accidental name mismatches are invisible โ your "override" simply becomes a new, never-called method.
private: visible only within the same class. protected: visible within the same class, same package, and all subclasses (regardless of package). public: visible everywhere. Use protected for fields that subclasses need to access directly without getters. Prefer keeping things private unless inheritance requires otherwise.
The diamond problem occurs in multiple inheritance when two parent classes have the same method, creating ambiguity about which version the grandchild inherits. Java avoids it by allowing only single class inheritance (extends one class). Interfaces with default methods can cause a similar issue โ Java resolves it by requiring the implementing class to explicitly override the ambiguous method.
๐ Quiz โ 5 questions
super(args) appear in a constructor?private. Can the child access it directly?@Override annotation โ what does it do?Next: polymorphism and method overriding โ
โ ๏ธ Common mistakes
super() automatically and fails. Always call super(args) explicitly as the first statement in every child constructor that needs non-default parent initialisation.@Override catches it at compile time. Without @Override, you'd silently define a new method instead of overriding โ the parent version keeps running and the bug is invisible.balance declared private in the parent is not visible in the child, even with super.balance. Declare such fields protected or provide getters/setters to expose them to subclasses.ca.transfer(...) calls the child override. If you need the parent version inside the subclass, call super.transfer(...) explicitly. From outside the class, there is no way to call the parent version directly.โ Key takeaways
extendsgives a subclass all parent fields and methods automatically.super(args)must be the first line in a child constructor to initialise the parent part.@Overrideis not optional โ always use it so the compiler catches signature mistakes.- Use
protectedfor fields subclasses need; keep thingsprivateotherwise. - Java allows only single class inheritance; multi-level chains are fine and common in real frameworks.
From inheritance to production Spring Boot
Join the Best AI Programs โ three 5-month courses, fully online, with placement assistance. Week 1 is free.
Explore our AI-era courses โ