Java Inheritance – Complete Guide

What is Inheritance in Java?

Inheritance is a key feature of Object-Oriented Programming (OOP) that allows a child class (subclass) to acquire the properties and behaviours of a parent class (superclass).

It improves code reusability.
It helps in method overriding (polymorphism).
It makes the code structured and easy to manage.

How Does Inheritance Work?

In Java, we use the extends keyword to implement inheritance:

javaCopyEdit// Parent class
class Animal {
    void sound() {
        System.out.println("Animals make different sounds");
    }
}

// Child class
class Dog extends Animal {
    void bark() {
        System.out.println("Dog barks");
    }
}

public class Main {
    public static void main(String[] args) {
        Dog myDog = new Dog();
        myDog.sound(); // Inherited method
        myDog.bark();  // Child class method
    }
}

Output:

goCopyEditAnimals make different sounds  
Dog barks  

Types of Inheritance in Java

1️⃣ Single Inheritance (One class inherits from another)

javaCopyEditclass Parent {
    void show() {
        System.out.println("This is Parent class");
    }
}

class Child extends Parent {
    void display() {
        System.out.println("This is Child class");
    }
}

public class Main {
    public static void main(String[] args) {
        Child obj = new Child();
        obj.show();
        obj.display();
    }
}

Output:

pythonCopyEditThis is Parent class  
This is Child class  

2️⃣ Multilevel Inheritance (One class inherits from another, which in turn inherits from another)

javaCopyEditclass GrandParent {
    void greet() {
        System.out.println("Hello from Grandparent");
    }
}

class Parent extends GrandParent {
    void show() {
        System.out.println("Hello from Parent");
    }
}

class Child extends Parent {
    void display() {
        System.out.println("Hello from Child");
    }
}

public class Main {
    public static void main(String[] args) {
        Child obj = new Child();
        obj.greet();
        obj.show();
        obj.display();
    }
}

Output:

csharpCopyEditHello from Grandparent  
Hello from Parent  
Hello from Child  

3️⃣ Hierarchical Inheritance (One parent class, multiple child classes)

javaCopyEditclass Animal {
    void eat() {
        System.out.println("Animals eat food");
    }
}

class Dog extends Animal {
    void bark() {
        System.out.println("Dog barks");
    }
}

class Cat extends Animal {
    void meow() {
        System.out.println("Cat meows");
    }
}

public class Main {
    public static void main(String[] args) {
        Dog myDog = new Dog();
        myDog.eat(); 
        myDog.bark();

        Cat myCat = new Cat();
        myCat.eat(); 
        myCat.meow();
    }
}

Output:

nginxCopyEditAnimals eat food  
Dog barks  
Animals eat food  
Cat meows  

4️⃣ Multiple Inheritance (via Interfaces)

Java does not support multiple inheritance with classes to avoid ambiguity, but it supports multiple inheritance using interfaces.

javaCopyEditinterface Animal {
    void eat();
}

interface Bird {
    void fly();
}

class Bat implements Animal, Bird {
    public void eat() {
        System.out.println("Bat eats insects");
    }

    public void fly() {
        System.out.println("Bat can fly");
    }
}

public class Main {
    public static void main(String[] args) {
        Bat myBat = new Bat();
        myBat.eat();
        myBat.fly();
    }
}

Output:

nginxCopyEditBat eats insects  
Bat can fly  

IS-A and HAS-A Relationships in Java

1️⃣ IS-A Relationship (Inheritance)

  • IS-A represents an inheritance relationship between a subclass and superclass.
  • It is implemented using the extends keyword (for classes) or implements (for interfaces).

Example:

javaCopyEditclass Animal {
}

class Dog extends Animal {
} 

Dog IS-A Animal because Dog inherits from Animal.

Another example using interfaces:

javaCopyEditinterface Vehicle {
}

class Car implements Vehicle {
}

Car IS-A Vehicle because it implements the Vehicle interface.

2️⃣ HAS-A Relationship (Composition)

  • HAS-A means one class contains an object of another class as a field.
  • It represents “composition” or “aggregation” instead of inheritance.

Example:

javaCopyEditclass Engine {
    void start() {
        System.out.println("Engine starts");
    }
}

class Car {
    Engine engine = new Engine(); // HAS-A Relationship

    void drive() {
        engine.start();
        System.out.println("Car is moving");
    }
}

public class Main {
    public static void main(String[] args) {
        Car myCar = new Car();
        myCar.drive();
    }
}

Output:

csharpCopyEditEngine starts  
Car is moving  

Car HAS-A Engine because the Car class contains an instance of Engine.


Method Overriding in Inheritance

Method overriding allows a subclass to provide a specific implementation of a method already defined in its superclass.

javaCopyEditclass Animal {
    void sound() {
        System.out.println("Animals make sounds");
    }
}

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

public class Main {
    public static void main(String[] args) {
        Animal myDog = new Dog();
        myDog.sound(); // Calls overridden method in Dog class
    }
}

Output:

nginxCopyEditDog barks  

Overriding allows dynamic method dispatch (runtime polymorphism).


Final Thoughts

Inheritance makes Java powerful and reusable. It helps in code organization, structure, and reusability.
IS-A (Inheritance) means one class is a type of another.
HAS-A (Composition) means one class contains another class.
Overriding enables runtime polymorphism.

Start using inheritance in Java to write cleaner, efficient, and structured code! 🚀

Leave a Reply

Your email address will not be published. Required fields are marked *