Java Inheritance

Introduction

Inheritance is one of the four fundamental pillars of Object-Oriented Programming (OOP) in Java, along with Encapsulation, Polymorphism, and Abstraction. It allows one class to acquire the properties and behaviors of another class, making code more reusable, organized, and maintainable.

In real life, children inherit characteristics from their parents. Similarly, in Java, a child class (subclass) inherits fields and methods from a parent class (superclass). This enables developers to reuse existing code instead of writing the same logic repeatedly.

Java implements inheritance using the extends keyword.


What is Inheritance?

Inheritance is the mechanism through which one class acquires the properties and methods of another class.

The existing class is called the parent class, superclass, or base class, while the new class is called the child class, subclass, or derived class.

Why Do We Need Inheritance?

Without inheritance, similar code must be written repeatedly.

Example without inheritance:


class Car {
    void start() {
        System.out.println("Vehicle Started");
    }
}
class Bike {
    void start() {
        System.out.println("Vehicle Started");
    }
}

The same start() method appears in multiple classes.

Using inheritance:


class Vehicle {
    void start() {
        System.out.println("Vehicle Started");
    }
}
class Car extends Vehicle {
}

The Car class automatically inherits the start() method.

Syntax

The extends keyword is used to create inheritance.


class ParentClass {
    // Fields and methods
}
class ChildClass extends ParentClass {
    // Additional fields and methods
}

Terminology

Term Description
Superclass The class whose members are inherited
Subclass The class that inherits from another class
Parent Class Another name for superclass
Child Class Another name for subclass
extends Keyword used to inherit a class

Simple Inheritance Example


class Animal {
    void eat() {
        System.out.println("Animal is eating");
    }
}
class Dog extends Animal {
    void bark() {
       System.out.println("Dog is barking");
    }
}
public class Main {
    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.eat();
        dog.bark();
    }
}

Output

Animal is eating
Dog is barking

The Dog class inherits the eat() method from Animal.

Types of Inheritance in Java

Java supports several inheritance types.

1. Single Inheritance

A child class inherits from one parent class.


 Animal
   │
   ▼
  Dog

Example:


class Animal {
    void eat() {
        System.out.println("Eating");
    }
}
class Dog extends Animal {
}

2. Multilevel Inheritance

A class inherits from another child class.


 Animal
   │
   ▼
  Dog
   │
   ▼
 Puppy

Example:


class Animal {
    void eat() {
       System.out.println("Eating");
    }
}
class Dog extends Animal {
    void bark() {
        System.out.println("Barking");
    }
}
class Puppy extends Dog {
}

The Puppy class inherits members from both Dog and Animal.

3. Hierarchical Inheritance

Multiple child classes inherit from the same parent class.


        Animal
       /      \
      ▼        ▼
    Dog      Cat

Example:


class Animal {
    void eat() {
        System.out.println("Eating");
    }
}
class Dog extends Animal {
}
class Cat extends Animal {
}

Both Dog and Cat inherit the eat() method.

4. Multiple Inheritance

Java does not support multiple inheritance with classes.

The following is not allowed:


// Not Allowed
class A {
}
class B {
}
// class C extends A, B { }

This restriction prevents ambiguity, often referred to as the Diamond Problem.

However, Java supports multiple inheritance through interfaces.

5. Hybrid Inheritance

Hybrid inheritance combines more than one inheritance type.

Java does not support hybrid inheritance using classes alone, but it can be achieved with interfaces

The extends Keyword

The extends keyword creates a parent-child relationship.

Example:


class Vehicle {
}
class Car extends Vehicle {
}

Everything that is accessible in Vehicle becomes available in Car.

Constructor Behavior in Inheritance

Constructors are not inherited.

However, when an object of the child class is created, the parent class constructor executes first.

Example:


class Animal {
    Animal() {
        System.out.println("Animal Constructor");
    }
}
class Dog extends Animal {
    Dog() {
        System.out.println("Dog Constructor");
    }
}
public class Main {
    public static void main(String[] args) {
        new Dog();
    }
}

Output

Animal Constructor
Dog Constructor

What Members Are Inherited?

The following table summarizes inheritance behavior.

Member Inherited?
Public fields Yes
Protected fields Yes
Public methods Public methods
Protected methods Yes
Constructors No
Private fields No(directly)
Private methods No
Static members yes (class members), though they are associated with the class rather than the object

Real-Life Example

Consider a Bank Management System.

The parent class:


class Account {
    double balance;
    void deposit(double amount) {
        balance += amount;
    }
}

Child classes:


class SavingsAccount extends Account {
}
class CurrentAccount extends Account {
}

Both account types inherit the deposit() method, avoiding duplicate code.

Advantages of Inheritance

Inheritance offers many benefits:

  • Promotes code reusability.
  • Reduces code duplication.
  • Simplifies maintenance.
  • Supports hierarchical relationships.
  • Makes applications easier to extend.
  • Forms the basis of polymorphism.
  • Encourages modular design.
  • Improves readability.

Best Practices

Follow these best practices when using inheritance:

📖
Best Practices:
  • Use inheritance only when there is a true “is-a” relationship.
  • Keep parent classes general and reusable.
  • Avoid deep inheritance hierarchies.
  • Prefer composition over inheritance when appropriate.
  • Use meaningful class names.
  • Override methods only when behavior genuinely changes.
  • Keep parent classes focused on common functionality.

Common Mistakes

Beginners often make these mistakes:

  • Confusing inheritance with composition.
  • Assuming constructors are inherited.
  • Trying to inherit from multiple classes.
  • Making everything public instead of using appropriate access modifiers.
  • Creating inheritance where there is no logical “is-a” relationship.
  • Forgetting that private members are not directly accessible in child classes.
  • Building unnecessarily deep inheritance trees.

Conclusion

Inheritance is one of the most powerful features of Java because it enables one class to reuse the fields and methods of another. By creating parent-child relationships, inheritance reduces duplicate code, improves maintainability, and makes applications easier to extend.

Java supports single, multilevel, and hierarchical inheritance using the extends keyword, while multiple and hybrid inheritance are achieved through interfaces.

Java Inheritance – Interview Questions

Q 1: What is inheritance in Java?
Ans: Inheritance allows a class to acquire properties and methods of another class.
Q 2: Which keyword is used for inheritance?
Ans: The extends keyword is used.
Q 3: Why is inheritance important?
Ans: It promotes code reusability and reduces redundancy.
Q 4: Does Java support multiple inheritance?
Ans: No, Java does not support multiple inheritance with classes.
Q 5: What is a superclass?
Ans: A class whose properties are inherited by another class.

Java Inheritance – Objective Questions (MCQs)

Q1. What is inheritance in Java?






Q2. Which keyword is used to inherit a class in Java?






Q3. Which of the following statements about inheritance is true?






Q4. Which class is the superclass of all classes in Java?






Q5. Which keyword is used to call the parent class constructor in Java?






Related Java Tutorials