๐ŸŽฏ 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 extends keyword 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 @Override annotation and why you should always use it
  • The protected access modifier for subclass-visible fields
  • Multi-level class hierarchies and when to use them
๐Ÿ’ผ Interview reality: "What is the difference between method overloading and overriding?" is asked in virtually every Java round. This lesson covers overriding via inheritance โ€” overloading was covered in Arc A (Lesson 05).

๐Ÿงฌ 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

๐Ÿฆ
Real-world scenario ยท SouthBank India
SouthBank India โ€” Arjun's loan product hierarchy (Mumbai)

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().

javac BasicInheritance.java && java BasicInheritance
BasicInheritance.javaLesson 10 ยท extends + super() + @Override
verified output
=== SouthBank India โ€” Savings Accounts === Account Owner Balance (โ‚น) ---------------------------------------------- SB-3001 Arjun โ‚น 133,125.00 [Savings 6.5%] SB-3002 Priya โ‚น 319,500.00 [Savings 6.5%] Total interest credited: โ‚น27,625.00

๐Ÿ” 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.

javac SuperDemo.java && java SuperDemo
SuperDemo.javaLesson 10 ยท super.method() and override
verified output
=== SouthBank India โ€” Current Account === Account[SB-CA-001 โ‚น10,000.00] [Current | OD: โ‚น50,000] Account[SB-SA-002 โ‚น5,000.00] Transferred โ‚น40,000.00 SB-CA-001 โ†’ SB-SA-002 (balance now โ‚น-30,000.00) Account[SB-CA-001 โ‚น-30,000.00] [Current | OD: โ‚น50,000] Account[SB-SA-002 โ‚น45,000.00]

๐Ÿ”— 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.

javac InheritanceChain.java && java InheritanceChain
InheritanceChain.javaLesson 10 ยท three-level class hierarchy
verified output
=== SouthBank India โ€” Home Loan Portfolio === HL-6001 principal: โ‚น 4,500,000 rate: 8.5% tenure: 20y property: Mumbai Total interest over 20 years: โ‚น7,650,000.00 HL-6002 principal: โ‚น 3,200,000 rate: 8.8% tenure: 15y property: Bangalore Total interest over 15 years: โ‚น4,200,000.00

๐Ÿ“‹ Quick reference โ€” inheritance keywords

KeywordPurpose
extendsDeclares parent class
super(args)Calls parent constructor (must be first)
super.method()Calls parent's version of an overridden method
@OverrideAnnotation that confirms method overrides parent
protectedVisible to subclasses and same package
final classCannot be extended
final methodCannot be overridden

๐ŸŽค Interview Q&A

EasyWhy must super() be the first statement in a constructor?
+

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.

EasyWhat is the difference between method overloading and overriding?
+

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.

MediumWhat does @Override do, and why should you always use it?
+

@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.

MediumHow does protected differ from private and public?
+

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.

HardWhat is the diamond problem, and how does Java avoid it?
+

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

Question 1 of 5
What keyword declares that one class inherits from another?
Question 2 of 5
Where must super(args) appear in a constructor?
Question 3 of 5
A parent field is private. Can the child access it directly?
Question 4 of 5
@Override annotation โ€” what does it do?
Question 5 of 5
Java supports:
0
/ 5
Quiz complete!

Next: polymorphism and method overriding โ†’

โš ๏ธ Common mistakes

Forgetting super() in child constructor
If the parent has no no-arg constructor, the compiler inserts super() automatically and fails. Always call super(args) explicitly as the first statement in every child constructor that needs non-default parent initialisation.
Typo in @Override method name
If you misspell the method name, @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.
Accessing private parent fields directly
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.
Expecting parent behaviour from an overridden method
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

  • extends gives a subclass all parent fields and methods automatically.
  • super(args) must be the first line in a child constructor to initialise the parent part.
  • @Override is not optional โ€” always use it so the compiler catches signature mistakes.
  • Use protected for fields subclasses need; keep things private otherwise.
  • Java allows only single class inheritance; multi-level chains are fine and common in real frameworks.
Africa & India's Best AI Academy
๐Ÿค– AI-first ๐ŸŽ“ Placement Assistance

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 โ†’