Java Programming Handbook

    What is Inheritance in Java?

    Inheritance is one of the fundamental concepts of Object-Oriented Programming (OOP) that allows a class to inherit the properties and behaviors (fields and methods) of another class. It promotes code reusability, modularity, and hierarchy in programming.

    Why Use Inheritance?#

    • Code Reusability: Reduces code duplication by allowing child classes to reuse existing code.
    • Modularity: Enhances maintainability by keeping related functionalities within a structured hierarchy.
    • Extensibility: Enables extending functionalities without modifying existing code.
    • Improved Readability: Provides a clear relationship between classes, making the program easier to understand.

    How Inheritance Works in Java?#

    In Java, inheritance is implemented using the extends keyword. The class that inherits another class is called a child class (subclass), and the class being inherited is called a parent class (superclass).

    Example: Basic Inheritance#

    // Superclass class Animal { void eat() { System.out.println("This animal eats food."); } } // Subclass inheriting Animal class class Dog extends Animal { void bark() { System.out.println("Dog barks."); } } public class Main { public static void main(String[] args) { Dog dog = new Dog(); dog.eat(); // Inherited method dog.bark(); } }

    Expected Output#

    This animal eats food. Dog barks.

    Types of Inheritance in Java#

    Java supports the following types of inheritance:

    1. Single Inheritance#

    A subclass inherits from a single superclass.

    class Vehicle { void move() { System.out.println("Vehicle is moving"); } } class Car extends Vehicle { void speed() { System.out.println("Car is speeding"); } }

    2. Multilevel Inheritance#

    A subclass inherits from another subclass.

    class Animal { void sleep() { System.out.println("Animal is sleeping"); } } class Mammal extends Animal { void walk() { System.out.println("Mammal walks"); } } class Dog extends Mammal { void bark() { System.out.println("Dog barks"); } }

    3. Hierarchical Inheritance#

    Multiple subclasses inherit from a single superclass.

    class Parent { void show() { System.out.println("Parent class"); } } class Child1 extends Parent { void display() { System.out.println("Child1 class"); } } class Child2 extends Parent { void display() { System.out.println("Child2 class"); } }

    4. Hybrid Inheritance (Using Interfaces)#

    Java does not support multiple inheritance directly, but it can be achieved using interfaces.

    interface A { void methodA(); } interface B { void methodB(); } class C implements A, B { public void methodA() { System.out.println("Method A"); } public void methodB() { System.out.println("Method B"); } }

    Why Multiple Inheritance is Not Supported in Java?#

    Multiple inheritance means a class inherits from more than one class. Java does not support it to avoid ambiguity and complexity.

    Example: The Diamond Problem#

    class A { void show() { System.out.println("Class A"); } } class B extends A { void show() { System.out.println("Class B"); } } class C extends A { void show() { System.out.println("Class C"); } } // If Java allowed multiple inheritance // class D extends B, C { // // Ambiguity: which show() method should be called? // }

    Java avoids this issue by allowing multiple inheritance only through interfaces.

    Conclusion#

    Inheritance is a key concept in Java that improves code reuse, readability, and maintainability. Java supports single, multilevel, hierarchical, and hybrid inheritance but does not support multiple inheritance due to potential ambiguities. Instead, interfaces provide a way to achieve multiple inheritance in Java. Understanding inheritance is essential for building scalable and maintainable applications.

    Last updated on Apr 09, 2025