Skip to main content
Java Basics
CHAPTER 15 Beginner

Inheritance in Java

Updated: May 17, 2026
5 min read

# CHAPTER 15

Inheritance in Java

1. Introduction

Inheritance allows a class to acquire the properties and methods of another class. It's one of the most powerful features of OOP, enabling code reuse and establishing natural hierarchies — just like how a child inherits traits from a parent.

2. Learning Objectives

  • Use the extends keyword.
  • Understand single, multilevel, and hierarchical inheritance.
  • Use the super keyword.
  • Override methods.
  • Understand the IS-A relationship.

3. The extends Keyword

java
123456789101112131415161718
// Parent (Super) class
class Animal {
    String name;
    void eat() { System.out.println(name + " is eating."); }
    void sleep() { System.out.println(name + " is sleeping."); }
}

// Child (Sub) class
class Dog extends Animal {
    void bark() { System.out.println(name + " says: Woof!"); }
}

// Usage
Dog d = new Dog();
d.name = "Buddy";
d.eat();   // Inherited from Animal
d.sleep(); // Inherited from Animal
d.bark();  // Own method

4. Types of Inheritance in Java

123456
Single:        Multilevel:      Hierarchical:
A               A                    A
|               |                   / \
B               B                  B   C
                |
                C

Note: Java does NOT support multiple inheritance with classes (a class cannot extend two classes). Use interfaces instead.

5. The super Keyword

Access parent class members from a child class:
java
1234567891011121314
class Vehicle {
    String brand = "Generic";
    void honk() { System.out.println("Beep!"); }
}

class Car extends Vehicle {
    String brand = "Toyota";
    
    void display() {
        System.out.println("Car brand: " + brand);        // Toyota
        System.out.println("Vehicle brand: " + super.brand); // Generic
        super.honk(); // Calls parent method
    }
}

6. super() in Constructors

java
12345678910111213141516
class Person {
    String name;
    Person(String name) {
        this.name = name;
        System.out.println("Person constructor");
    }
}

class Student extends Person {
    int grade;
    Student(String name, int grade) {
        super(name); // MUST be the first statement
        this.grade = grade;
        System.out.println("Student constructor");
    }
}

7. Method Overriding

A child class provides its own implementation of a parent method:
java
12345678910111213
class Animal {
    void sound() { System.out.println("Some sound"); }
}

class Cat extends Animal {
    @Override
    void sound() { System.out.println("Meow!"); }
}

class Dog extends Animal {
    @Override
    void sound() { System.out.println("Woof!"); }
}

8. The @Override Annotation

Tells the compiler you intend to override a parent method. If you make a typo, the compiler catches it:
java
12
@Override
void sound() { ... } // Compiler verifies this method exists in parent

9. The final Keyword with Inheritance

  • final class — Cannot be extended.
  • final method — Cannot be overridden.
java
12
final class ImmutableClass { } // No class can extend this
// class Child extends ImmutableClass {} // ERROR!

10. MCQ Quiz with Answers

Question 1

Which keyword establishes inheritance?

Question 2

Java supports multiple inheritance via:

Question 3

super() must be:

Question 4

Method overriding means:

Question 5

Which annotation marks an overriding method?

Question 6

Can a final class be extended?

Question 7

What does super.method() do?

Question 8

If no constructor is defined in child, what happens?

Question 9

IS-A relationship in inheritance means:

Question 10

Can a child class add new methods?

11. Interview Questions

  • Q: Why doesn't Java support multiple inheritance with classes?
  • Q: What is the diamond problem?
  • Q: Difference between method overloading and overriding?
  • Q: When should you use inheritance vs composition?

12. Summary

Inheritance allows code reuse through parent-child relationships using extends. super accesses parent members. Method overriding lets children customize inherited behavior. Java avoids multiple class inheritance to prevent the diamond problem, but supports it through interfaces.

13. Next Chapter Recommendation

In Chapter 16: Polymorphism in Java, we'll explore how a single reference can take multiple forms at runtime.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·