Inheritance
Reading Time: 10 Minutes
Difficulty: Beginner
Topic Summaryโ
Inheritance is one of the four pillars of OOP. It lets a new class (child) automatically get all the fields and methods of an existing class (parent), and then add or change things on top. It's the ultimate code reuse mechanism โ write something once in the parent, and all children get it for free.
What You'll Learnโ
- What inheritance is and why it matters
- How to use the
extendskeyword - What the IS-A relationship means
- What gets inherited and what doesn't
- A multi-level example: Animal โ Dog โ GoldenRetriever
Prerequisitesโ
- Classes and Objects (Lesson 02)
- Constructors (Lesson 03)
- this Keyword (Lesson 04)
Explanationโ
What Is Inheritance?โ
Imagine you're designing a game with many types of characters: Warriors, Mages, Archers. All of them have a name, health points, and can move. Instead of writing name, hp, and move() three times in three separate classes, you write it once in a Character class and let the others inherit from it.
Inheritance = "I already have everything my parent has, plus my own stuff."
In Java, inheritance is implemented with the extends keyword.
The extends Keywordโ
class Animal { // Parent class (superclass)
String name;
int age;
void eat() {
System.out.println(name + " is eating.");
}
void sleep() {
System.out.println(name + " is sleeping.");
}
}
class Dog extends Animal { // Child class (subclass)
String breed;
void bark() {
System.out.println(name + " says: Woof!"); // 'name' inherited from Animal!
}
}
Dog extends Animal means: Dog has everything Animal has, PLUS its own breed field and bark() method.
The IS-A Relationshipโ
Inheritance models an IS-A relationship:
- A Dog IS-A Animal โ
- A GoldenRetriever IS-A Dog โ
- A Car IS-A Animal โ (doesn't make sense โ don't extend it!)
The rule: only use inheritance when the child truly "IS A" more specific version of the parent.
What Gets Inherited?โ
| Gets Inherited | Does NOT Get Inherited |
|---|---|
| Public fields | Private fields (but accessible via public getters) |
| Public methods | Private methods |
| Protected fields | Constructors |
| Protected methods | Static members (inherited differently) |
Constructors are NOT inherited. The child class must define its own constructors and call the parent's constructor using super().
Single Inheritance in Javaโ
Java supports single inheritance โ a class can extend only ONE parent class. This avoids the "diamond problem" seen in languages like C++.
class A extends B, C { } // COMPILE ERROR โ not allowed in Java!
class A extends B { } // OK โ single inheritance only
However, a class can implement multiple interfaces (covered in Lesson 10).
Multi-Level Inheritanceโ
A child can itself become a parent. This creates a chain:
class Animal { ... } // grandparent
class Dog extends Animal { ... } // parent
class GoldenRetriever extends Dog { ... } // child
A GoldenRetriever object has ALL fields and methods from all three classes!
The Object Classโ
In Java, every class automatically extends Object (from java.lang) if no parent is explicitly specified. So Object is the root of all Java classes. It provides methods like toString(), equals(), and hashCode().
Real-World Analogyโ
Think of family inheritance.
Your grandfather established a business. Your parent inherited the business (and all its assets). You inherit from your parent โ getting the business, the assets, AND anything new your parent added. Each generation adds their own thing on top of what they inherited.
In Java, that's exactly how class inheritance works โ each child gets everything above it in the family tree, and adds its own specialization.
Code Exampleโ
// Level 1: Animal (grandparent)
class Animal {
String name;
int age;
Animal(String name, int age) {
this.name = name;
this.age = age;
}
void eat() {
System.out.println(name + " is eating.");
}
void breathe() {
System.out.println(name + " is breathing.");
}
void displayInfo() {
System.out.println("Name: " + name + ", Age: " + age);
}
}
// Level 2: Dog extends Animal (parent)
class Dog extends Animal {
String breed;
Dog(String name, int age, String breed) {
super(name, age); // calls Animal's constructor
this.breed = breed;
}
void bark() {
System.out.println(name + " says: Woof! Woof!");
}
void fetch() {
System.out.println(name + " fetched the ball!");
}
}
// Level 3: GoldenRetriever extends Dog (child)
class GoldenRetriever extends Dog {
String furColor;
GoldenRetriever(String name, int age, String furColor) {
super(name, age, "Golden Retriever"); // calls Dog's constructor
this.furColor = furColor;
}
void swim() {
System.out.println(name + " is swimming โ Golden Retrievers love water!");
}
@Override
void displayInfo() {
super.displayInfo(); // calls Animal's displayInfo
System.out.println("Breed: " + breed + ", Fur: " + furColor);
}
}
public class Main {
public static void main(String[] args) {
GoldenRetriever buddy = new GoldenRetriever("Buddy", 3, "Golden");
// Inherited from Animal
buddy.eat();
buddy.breathe();
// Inherited from Dog
buddy.bark();
buddy.fetch();
// Own method
buddy.swim();
// Overridden displayInfo
buddy.displayInfo();
}
}
Outputโ
Buddy is eating.
Buddy is breathing.
Buddy says: Woof! Woof!
Buddy fetched the ball!
Buddy is swimming โ Golden Retrievers love water!
Name: Buddy, Age: 3
Breed: Golden Retriever, Fur: Golden
Common Mistakesโ
- โ Mistake: Extending a class just to reuse code when there's no real IS-A relationship โ โ Fix: Only extend when the child truly IS-A type of the parent. Use composition (has-a) otherwise.
- โ Mistake: Forgetting to call
super(...)in the child constructor โ โ Fix: If the parent has a parameterized constructor, the child MUST callsuper(...)as the first statement. - โ Mistake: Trying to extend multiple classes โ โ Fix: Java only allows single inheritance. Use interfaces for multiple type contracts.
Best Practicesโ
- Use inheritance only for true IS-A relationships (a
DogIS-AAnimal) - Prefer shallow inheritance hierarchies โ deep chains become hard to follow
- If you find yourself duplicating code across unrelated classes, that's when inheritance (or composition) helps
- Use
@Overrideannotation when overriding methods โ it helps the compiler catch errors
Interview Questionsโ
Q: What is inheritance in Java?
A: Inheritance is an OOP mechanism where a child class (subclass) acquires the fields and methods of a parent class (superclass) using the extends keyword. It enables code reuse and represents an IS-A relationship.
Q: Does Java support multiple inheritance?
A: Java does NOT support multiple inheritance through classes (a class can only extend one class). This avoids the "diamond problem." However, Java supports multiple inheritance through interfaces โ a class can implement multiple interfaces.
Q: What is the difference between IS-A and HAS-A relationships?
A: IS-A (inheritance): Dog IS-A Animal โ class Dog extends Animal. HAS-A (composition): Car HAS-A Engine โ class Car { Engine engine; }. IS-A is implemented through inheritance; HAS-A is implemented by making one class a field of another.
Quick Revisionโ
โ Inheritance: child class gets all public/protected fields and methods from parent
โ Use extends keyword โ only one class allowed (single inheritance)
โ Models IS-A relationship: Dog IS-A Animal
โ Constructors are NOT inherited โ use super() to call parent constructor
โ Multi-level inheritance is allowed: A โ B โ C
โ All Java classes implicitly extend Object
Related Topicsโ
- super Keyword
- Method Overriding
- Polymorphism
- Abstraction
Next Lessonโ
06 - The super Keyword