100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Java

Inheritance in Java

Understand Java inheritance using the extends keyword, the super keyword, single inheritance rules, and the Object root class.

OOP BasicsBeginner10 min readJul 7, 2026
Analogies

1. Introduction

Inheritance is an OOP mechanism that allows one class (the subclass or child class) to acquire the fields and methods of another class (the superclass or parent class), using the extends keyword. It promotes code reuse and establishes an 'is-a' relationship between classes.

🏏

Cricket analogy: Inheritance is like a young all-rounder joining a franchise's academy and automatically picking up the team's core training methods and playbook via extends, establishing that he 'is-a' member of that franchise's system while adding his own signature shots.

In Java, every class implicitly extends java.lang.Object if it does not explicitly extend another class, making Object the root of the entire class hierarchy. This means every Java object automatically inherits methods like toString(), equals(), and hashCode().

🏏

Cricket analogy: Every Java class implicitly extends Object, like every cricketer implicitly belonging to the ICC's foundational rules even without stating it, automatically inheriting baseline behaviors — like how every player automatically has a standard scorecard entry (toString()), a way to be compared to another player (equals()), and a unique registration number (hashCode()).

2. Syntax

java
class Parent {
    // parent fields and methods
}

class Child extends Parent {
    // Child inherits Parent's non-private members
    // and can add its own fields/methods

    Child() {
        super();   // calls Parent's constructor (implicit if omitted)
    }
}

3. Explanation

A subclass inherits all non-private fields and methods of its superclass and can add new members or override existing methods to provide specialized behavior. The super keyword is used to explicitly access the parent class's fields, methods, or constructor from within the subclass.

🏏

Cricket analogy: A subclass like FastBowler inherits non-private fields and methods from the Bowler superclass but can override bowl() with a yorker specialty, and use super.bowl() to still fall back on the parent's standard delivery technique when needed.

Critical exam point: Java does NOT support multiple inheritance of classes — a class can extend only ONE superclass (class C extends A, B is illegal). However, Java DOES support multiple inheritance of type via interfaces — a class can implement multiple interfaces. This avoids the 'diamond problem' associated with multiple class inheritance.

Every class in Java, if it doesn't extend another class explicitly, implicitly extends java.lang.Object — making Object the ultimate root of every class hierarchy.

4. Example

java
public class Vehicle {
    String brand = "Generic";

    void honk() {
        System.out.println("Beep beep!");
    }
}

class Car extends Vehicle {
    int wheels = 4;

    void display() {
        System.out.println("Brand: " + super.brand + ", Wheels: " + wheels);
        super.honk();  // calling parent's method explicitly
    }
}

class Demo {
    public static void main(String[] args) {
        Car myCar = new Car();
        myCar.display();
        myCar.honk(); // inherited method usable directly too
    }
}

5. Output

text
Brand: Generic, Wheels: 4
Beep beep!
Beep beep!

6. Key Takeaways

  • Inheritance lets a subclass reuse fields and methods of a superclass via extends.
  • Java supports only single inheritance for classes — a class can extend just one superclass.
  • Multiple inheritance of type is possible in Java only through interfaces, not classes.
  • The super keyword accesses the parent class's fields, methods, or constructor.
  • Every class implicitly extends java.lang.Object if no other superclass is specified.

Practice what you learned

Was this page helpful?

Topics covered

#Java#JavaProgrammingStudyNotes#Programming#InheritanceInJava#Inheritance#Syntax#Explanation#Example#OOP#StudyNotes#SkillVeris