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:
|
0 1 2 3 4 5 6 7 8 |
class Child extends Parent { // additional fields & methods } |
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
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
// Parent class public class Vehicle { String brand; int year; public Vehicle(String brand, int year) { this.brand = brand; this.year = year; } public void startEngine() { System.out.println(brand + " engine started!"); } public void displayInfo() { System.out.println("Brand: " + brand + ", Year: " + year); } } // Child class public class Car extends Vehicle { int doors; String color; // Constructor using super public Car(String brand, int year, int doors, String color) { super(brand, year); // Call parent's constructor this.doors = doors; this.color = color; } // Additional method public void honk() { System.out.println(brand + " says: Beep Beep!"); } // Override parent's method @Override public void displayInfo() { super.displayInfo(); // Call parent's version System.out.println("Doors: " + doors + ", Color: " + color); } } |
Main.java (test it):
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public class Main { public static void main(String[] args) { Car myCar = new Car("Toyota", 2023, 4, "Red"); myCar.startEngine(); // Inherited from Vehicle myCar.honk(); // Own method myCar.displayInfo(); // Overridden method } } |
Output:
|
0 1 2 3 4 5 6 7 8 9 |
Toyota engine started! Toyota says: Beep Beep! Brand: Toyota, Year: 2023 Doors: 4, Color: Red |
4. Multilevel Inheritance (Chain of Inheritance)
Example: Animal → Mammal → Dog
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
// Grandparent public class Animal { String name; public Animal(String name) { this.name = name; } public void eat() { System.out.println(name + " is eating."); } } // Parent public class Mammal extends Animal { public Mammal(String name) { super(name); } public void walk() { System.out.println(name + " is walking on 4 legs."); } } // Child public class Dog extends Mammal { public Dog(String name) { super(name); } public void bark() { System.out.println(name + " says: Woof Woof!"); } } |
Test:
|
0 1 2 3 4 5 6 7 8 9 |
Dog myDog = new Dog("Bruno"); myDog.eat(); // From Animal myDog.walk(); // From Mammal myDog.bark(); // Own method |
Output:
|
0 1 2 3 4 5 6 7 8 |
Bruno is eating. Bruno is walking on 4 legs. Bruno says: Woof Woof! |
5. Hierarchical Inheritance (One Parent → Multiple Children)
Example: Vehicle → Car, Bike
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
// Parent public class Vehicle { ... } // same as above // Child 1 public class Car extends Vehicle { ... } // same as above // Child 2 public class Bike extends Vehicle { int wheels = 2; public Bike(String brand, int year) { super(brand, year); } public void ride() { System.out.println(brand + " bike is zooming!"); } } |
Test:
|
0 1 2 3 4 5 6 7 8 9 10 11 |
Car car = new Car("Honda", 2024, 4, "Blue"); Bike bike = new Bike("Hero", 2022); car.startEngine(); bike.startEngine(); // Both inherit this method bike.ride(); |
6. The super Keyword (Very Important!)
super refers to the immediate parent class.
Uses:
- Call parent constructor: super(args); → must be first line
- Call parent method: super.method();
- Access parent field: super.field (rare)
Example from above:
|
0 1 2 3 4 5 6 7 8 9 10 |
public Car(String brand, int year, int doors, String color) { super(brand, year); // Calls Vehicle's constructor this.doors = doors; this.color = color; } |
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):
|
0 1 2 3 4 5 6 7 8 9 10 |
@Override public void displayInfo() { super.displayInfo(); // Call parent's version System.out.println("Doors: " + doors + ", Color: " + color); } |
Another Example: Animal sound
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
class Animal { public void sound() { System.out.println("Some generic animal sound"); } } class Dog extends Animal { @Override public void sound() { System.out.println("Woof Woof!"); } } class Cat extends Animal { @Override public void sound() { System.out.println("Meow!"); } } |
Test (Polymorphism preview):
|
0 1 2 3 4 5 6 7 8 9 10 |
Animal myPet1 = new Dog(); // Upcasting Animal myPet2 = new Cat(); myPet1.sound(); // Woof Woof! myPet2.sound(); // Meow! |
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!)
- Basic: Create Person (name, age) → Employee extends Person (salary, jobTitle). Add constructors and overridden displayInfo().
- Medium: Multilevel: LivingBeing → Human → Student. Each adds one method.
- Advanced: Hierarchical: Shape (area()) → Circle, Rectangle, Triangle. Override area() in each.
- Fun: Create BankAccount → SavingsAccount, CurrentAccount. Override withdraw() with different rules.
- Challenge: Fix this code:
Java01234567891011class Parent {public void show() { System.out.println("Parent"); }}class Child extends Parent {public void show(int x) { System.out.println("Child"); } // Is this override?}
You’re doing amazing! Inheritance is the backbone of large Java applications — now you can build real class hierarchies.
