Chapter 11: Inheritance

Inheritance lets you reuse code, create hierarchies of related classes, and build real-world relationships like “a Car is a Vehicle” or “a Student is a Person”.

Imagine we’re sitting together in your favorite Mumbai café — it’s evening, the lights are on, and I’m going to explain inheritance like I’m teaching my younger brother who’s just starting to understand the real power of Java.

We’ll go super slowly, with lots of real-life analogies, complete runnable programs, step-by-step breakdowns, tables, common mistakes with fixes, and plenty of examples you can copy-paste and run right now.

Let’s dive in!

1. What is Inheritance? (The Big Idea)

Inheritance allows a child class (subclass/derived class) to inherit fields and methods from a parent class (superclass/base class). The child can reuse, extend, or modify what it inherits.

Real-life analogy:

  • Parent class = Vehicle (has wheels, engine, move() method)
  • Child class = Car (inherits wheels, engine, move() → but adds doors, AC)
  • Child class = Bike (inherits wheels, engine, move() → but adds no doors, has handle)

Syntax:

Java

Key Benefits:

  • Code reuse — write once, use many times
  • Hierarchy — organize related classes logically
  • Polymorphism — treat child as parent (more in next chapter)

2. Types of Inheritance in Java

Java supports these types:

Type Description Diagram / Example Supported in Java?
Single One child inherits from one parent Vehicle → Car Yes
Multilevel Chain: Grandparent → Parent → Child Animal → Mammal → Dog Yes
Hierarchical One parent has multiple children Vehicle → (Car, Bike, Truck) Yes
Multiple Child inherits from multiple parents Not allowed (diamond problem) No (use interfaces)
Hybrid Mix of above Partial (via interfaces)

3. Single Inheritance (Most Common)

Example 1: Vehicle → Car

Java

Main.java (test it):

Java

Output:

text

4. Multilevel Inheritance (Chain of Inheritance)

Example: Animal → Mammal → Dog

Java

Test:

Java

Output:

text

5. Hierarchical Inheritance (One Parent → Multiple Children)

Example: Vehicle → Car, Bike

Java

Test:

Java

6. The super Keyword (Very Important!)

super refers to the immediate parent class.

Uses:

  1. Call parent constructor: super(args); → must be first line
  2. Call parent method: super.method();
  3. Access parent field: super.field (rare)

Example from above:

Java

7. Method Overriding (Runtime Polymorphism)

Rules:

  • Same name, return type, parameters as parent method
  • Child class must have @Override annotation (good practice)
  • Access modifier cannot be more restrictive

Example (already in Car class):

Java

Another Example: Animal sound

Java

Test (Polymorphism preview):

Java

Quick Recap Table (Your Cheat Sheet)

Concept Key Points / Syntax Example
Single Inheritance class Child extends Parent Vehicle → Car
Multilevel Chain: A → B → C Animal → Mammal → Dog
Hierarchical One parent → many children Vehicle → Car, Bike, Truck
super() Call parent constructor (first line) super(brand, year);
super.method() Call parent’s overridden method super.displayInfo();
Method Overriding @Override same signature in child sound() in Dog overrides Animal

Common Mistakes & Fixes

Mistake Problem Fix
Forgetting super() in child constructor Parent fields not initialized Call super(args); first
Changing return type in override Compile error Keep same return type
Making override method private Not overriding (hiding instead) Keep same or wider access (public > protected)
No @Override annotation Risk of accidental overload Always add @Override
Multiple inheritance (extends A, B) Compile error Use interfaces instead

Homework for You (Practice to Master!)

  1. Basic: Create Person (name, age) → Employee extends Person (salary, jobTitle). Add constructors and overridden displayInfo().
  2. Medium: Multilevel: LivingBeing → Human → Student. Each adds one method.
  3. Advanced: Hierarchical: Shape (area()) → Circle, Rectangle, Triangle. Override area() in each.
  4. Fun: Create BankAccount → SavingsAccount, CurrentAccount. Override withdraw() with different rules.
  5. Challenge: Fix this code:
    Java

You’re doing amazing! Inheritance is the backbone of large Java applications — now you can build real class hierarchies.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *