Types of Inheritance in Java


Types of Inheritance In Java, there are four types of inheritance: single inheritance, multiple inheritance (achieved through interfaces), multilevel inheritance, and hierarchical inheritance. Let’s explore each type along with key differences and programming examples for better understanding.

Questions covered in this article:

  1. Java Inheritance Tutorial for Beginners.
  2. Java Object-Oriented Programming (OOP) Concepts.
  3. Types of inheritance in Java.
  4. Java inheritance with examples.

Types of Inheritance in Java: A Tabular Comparison

AspectSingle InheritanceMultiple Inheritance through InterfacesMultilevel InheritanceHierarchical Inheritance
DefinitionA class can inherit from only one superclass.A class can implement multiple interfaces to inherit behaviors from multiple sources.A class can extend another class, which in turn extends another class.Multiple subclasses inherit from a single superclass.
Keyword Usageextends keywordimplements keywordextends keywordextends keyword
Number of ParentsOne superclassMultiple interfacesOne superclass and one parent classOne superclass and multiple subclasses
Code ReusabilityLimited code reusability due to single inheritance.Promotes code reusability and flexibility by inheriting from multiple sources.Enhances code reusability through a chain of classes.Encourages code reusability for multiple subclasses.
ExampleSingle Inheritance Example (Vehicle -> Car)Multiple Inheritance Example (Car -> Flying, Swimming)Multilevel Inheritance Example (Vehicle -> Car -> SportsCar)Hierarchical Inheritance Example (Shape -> Circle, Rectangle)

Types of Inheritance in Java with coding example

1. Single Inheritance:


Single inheritance is a type of inheritance where a class can inherit from only one superclass. In other words, a class can have only one direct parent class.

Key Difference:
A class can extend only one superclass, limiting its ability to inherit properties and behaviors from multiple sources.

Example:

// Superclass
class Animal {
    void makeSound() {
        System.out.println("Animal makes a sound");
    }
}

// Subclass inheriting from the 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.makeSound(); // Output: Animal makes a sound
        dog.bark(); // Output: Dog barks
    }
}

2. Multiple Inheritance (Through Interfaces):


Java achieves multiple inheritance through interfaces, where a class can implement multiple interfaces to inherit behaviors from multiple sources.

Key Difference:
A class can implement multiple interfaces, allowing it to inherit behaviors from several sources, overcoming the limitation of single inheritance.

Example:

// Interface 1
interface Flying {
    void fly();
}

// Interface 2
interface Swimming {
    void swim();
}

// Subclass implementing multiple interfaces
class Duck implements Flying, Swimming {
    @Override
    public void fly() {
        System.out.println("Duck flies");
    }

    @Override
    public void swim() {
        System.out.println("Duck swims");
    }
}

public class Main {
    public static void main(String[] args) {
        Duck duck = new Duck();
        duck.fly(); // Output: Duck flies
        duck.swim(); // Output: Duck swims
    }
}

3. Multilevel Inheritance:


Multilevel inheritance is a type of inheritance where a class extends another class, which in turn extends another class.

Key Difference:
Classes are arranged in a chain, where each class inherits properties and behaviors from its parent class, forming a hierarchical relationship.

Example:

// Superclass
class Vehicle {
    void drive() {
        System.out.println("Vehicle drives");
    }
}

// Subclass inheriting from the Vehicle class
class Car extends Vehicle {
    void start() {
        System.out.println("Car starts");
    }
}

// Subclass inheriting from the Car class
class SportsCar extends Car {
    void accelerate() {
        System.out.println("SportsCar accelerates");
    }
}

public class Main {
    public static void main(String[] args) {
        SportsCar sportsCar = new SportsCar();
        sportsCar.drive(); // Output: Vehicle drives
        sportsCar.start(); // Output: Car starts
        sportsCar.accelerate(); // Output: SportsCar accelerates
    }
}

4. Hierarchical Inheritance:


Hierarchical inheritance is a type of inheritance where multiple subclasses inherit from a single superclass.

Key Difference:
The superclass serves as a common base for multiple subclasses, allowing code reuse and promoting a hierarchical class structure.

Example:

// Superclass
class Shape {
    void draw() {
        System.out.println("Drawing a shape");
    }
}

// Subclass inheriting from the Shape class
class Circle extends Shape {
    void draw() {
        System.out.println("Drawing a circle");
    }
}

// Another subclass inheriting from the Shape class
class Rectangle extends Shape {
    void draw() {
        System.out.println("Drawing a rectangle");
    }
}

public class Main {
    public static void main(String[] args) {
        Circle circle = new Circle();
        circle.draw(); // Output: Drawing a circle

        Rectangle rectangle = new Rectangle();
        rectangle.draw(); // Output: Drawing a rectangle
    }
}

Types of Inheritance in Java Conclusion:


Understanding the different types of inheritance in Java and their key differences is crucial for effective object-oriented programming. Each type of inheritance serves a unique purpose and offers distinct advantages. By leveraging inheritance effectively, developers can create well-organized and maintainable Java applications that promote code reusability and flexibility.

Inheritance | Where to use Which

Real-life Java Inheritance Scenarios:

Single Inheritance Scenario:

Consider a scenario where you’re building a banking application. You can have a base class called Account that holds common attributes and methods for different types of bank accounts like savings and checking accounts.

Multiple Inheritance (Interface) Scenario:

Imagine creating a multimedia application. You might define interfaces like Playable and Displayable. A class representing a video player can then implement both interfaces, inheriting methods for playing and displaying content.

Hierarchical Inheritance Scenario:

In a game development scenario, you might have a base class Character representing game characters’ common attributes and behaviors. Subclasses like PlayerCharacter and EnemyCharacter can inherit from Character while adding their unique features

Related Articles:

Leave a Comment