types of inheritance in java

types of inheritance in java

In Java, inheritance can be classified into different types based on the relationships between classes. The main types of inheritance are:

1. Single Inheritance:

2. Multilevel Inheritance:

3. Hierarchical Inheritance:

4. Hybrid Inheritance:

5. Multiple Inheritance (through Interfaces):

types of inheritance in java
1. Single Inheritance:

In single inheritance, a class can extend only one superclass. Java supports single inheritance for classes, meaning a class can have only one direct parent class.

types of inheritance in java
Example
// Parent class (superclass)
class Animal {
    void eat() {
        System.out.println("Animal is eating.");
    }

    void sleep() {
        System.out.println("Animal is sleeping.");
    }
}

// Child class (subclass) inheriting from Animal
class Dog extends Animal {
    void bark() {
        System.out.println("Dog is barking.");
    }
}

public class Main {
    public static void main(String[] args) {
        // Creating an instance of the Dog class
        Dog myDog = new Dog();

        // Accessing methods from both Animal and Dog classes
        myDog.eat();  // Inherited from Animal
        myDog.sleep();  // Inherited from Animal
        myDog.bark();   // Specific to Dog
    }
}

In this example:

  • Animal is the parent class (superclass) with methods eat() and sleep().
  • Dog is the child class (subclass) that extends Animal. It inherits the methods eat() and sleep() from the Animal class and adds its own method bark().
  • The main method demonstrates creating an instance of the Dog class and calling methods from both the Animal and Dog classes.

This represents a simple example of single inheritance, where a subclass extends a single superclass.

2. Multilevel Inheritance:

In multilevel inheritance, a class serves as a superclass for another class, and that subclass, in turn, becomes the superclass for another class.

types of inheritance in java

In this example, C inherits from B, which, in turn, inherits from A.

Example
// Grandparent class (superclass)
class Animal {
    void eat() {
        System.out.println("Animal is eating.");
    }

    void sleep() {
        System.out.println("Animal is sleeping.");
    }
}

// Parent class (subclass) inheriting from Animal
class Dog extends Animal {
    void bark() {
        System.out.println("Dog is barking.");
    }
}

// Child class (subclass) inheriting from Dog
class Bulldog extends Dog {
    void guard() {
        System.out.println("Bulldog is guarding.");
    }
}

public class Main {
    public static void main(String[] args) {
        // Creating an instance of the Bulldog class
        Bulldog myBulldog = new Bulldog();

        // Accessing methods from Animal, Dog, and Bulldog classes
        myBulldog.eat();    // Inherited from Animal
        myBulldog.sleep();  // Inherited from Animal
        myBulldog.bark();   // Inherited from Dog
        myBulldog.guard();  // Specific to Bulldog
    }
}

In this example:

  • Animal is the grandparent class (superclass) with methods eat() and sleep().
  • Dog is the parent class (subclass) that extends Animal. It inherits the methods eat() and sleep() from the Animal class and adds its own method bark().
  • Bulldog is the child class (subclass) that extends Dog. It inherits the methods eat(), sleep(), and bark() from the Dog class and adds its own method guard().
  • The main method demonstrates creating an instance of the Bulldog class and calling methods from the Animal, Dog, and Bulldog classes.

This represents a multilevel inheritance scenario where a class extends another class, which in turn is extended by another class.

3. Hierarchical Inheritance:

In hierarchical inheritance, multiple classes inherit from a common superclass. It forms a tree-like structure.

types of inheritance in java

Both B and C inherit from a common superclass A.

Example
// Parent class (superclass)
class Animal {
    void eat() {
        System.out.println("Animal is eating.");
    }

    void sleep() {
        System.out.println("Animal is sleeping.");
    }
}

// Child class 1 (subclass) inheriting from Animal
class Dog extends Animal {
    void bark() {
        System.out.println("Dog is barking.");
    }
}

// Child class 2 (subclass) inheriting from Animal
class Cat extends Animal {
    void meow() {
        System.out.println("Cat is meowing.");
    }
}

public class Main {
    public static void main(String[] args) {
        // Creating an instance of the Dog class
        Dog myDog = new Dog();
        myDog.eat();    // Inherited from Animal
        myDog.sleep();  // Inherited from Animal
        myDog.bark();   // Specific to Dog

        System.out.println(); // Separating output for clarity

        // Creating an instance of the Cat class
        Cat myCat = new Cat();
        myCat.eat();    // Inherited from Animal
        myCat.sleep();  // Inherited from Animal
        myCat.meow();   // Specific to Cat
    }
}

In this example:

  • Animal is the parent class (superclass) with methods eat() and sleep().
  • Dog is one child class (subclass) that extends Animal. It inherits the methods eat() and sleep() from the Animal class and adds its own method bark().
  • Cat is another child class (subclass) that extends Animal. It inherits the methods eat() and sleep() from the Animal class and adds its own method meow().
  • The main method demonstrates creating instances of both the Dog and Cat classes and calling methods from the Animal, Dog, and Cat classes.

This represents a hierarchical inheritance scenario where multiple classes inherit from a common superclass.

4. Hybrid Inheritance:

Hybrid inheritance is a combination of two or more types of inheritance mentioned above. For example, a class might exhibit multiple inheritance through interfaces, and within those interfaces, it might have multilevel or hierarchical inheritance.

types of inheritance in java
Example
// Interface with a method
interface Walkable {
    void walk();
}

// Grandparent class (superclass)
class Animal {
    void eat() {
        System.out.println("Animal is eating.");
    }

    void sleep() {
        System.out.println("Animal is sleeping.");
    }
}

// Parent class (subclass) inheriting from Animal and implementing Walkable interface
class Dog extends Animal implements Walkable {
    void bark() {
        System.out.println("Dog is barking.");
    }

    // Implementing the walk() method from the Walkable interface
    @Override
    public void walk() {
        System.out.println("Dog is walking.");
    }
}

// Parent class (subclass) inheriting from Animal and implementing Walkable interface
class Cat extends Animal implements Walkable {
    void meow() {
        System.out.println("Cat is meowing.");
    }

    // Implementing the walk() method from the Walkable interface
    @Override
    public void walk() {
        System.out.println("Cat is walking.");
    }
}

// Child class (subclass) inheriting from Dog
class Bulldog extends Dog {
    void guard() {
        System.out.println("Bulldog is guarding.");
    }
}

public class Main {
    public static void main(String[] args) {
        // Creating an instance of the Bulldog class
        Bulldog myBulldog = new Bulldog();

        // Accessing methods from Animal, Dog, and Bulldog classes
        myBulldog.eat();    // Inherited from Animal
        myBulldog.sleep();  // Inherited from Animal
        myBulldog.bark();   // Inherited from Dog
        myBulldog.walk();   // Implemented from Walkable interface in Dog
        myBulldog.guard();  // Specific to Bulldog
    }
}

In this example:

  • Animal is the grandparent class (superclass) with methods eat() and sleep().
  • Dog and Cat are parent classes (subclasses) that extend Animal and implement the Walkable interface.
  • Bulldog is a child class (subclass) that extends Dog.
  • The Walkable interface defines a walk() method that both Dog and Cat implement.
  • The main method demonstrates creating an instance of the Bulldog class and calling methods from the Animal, Dog, Bulldog, and the Walkable interface.

This example illustrates a combination of single inheritance, interface-based multiple inheritance, and hierarchical inheritance, making it a form of hybrid inheritance.

5. Multiple Inheritance (through Interfaces):

Java does not support multiple inheritance for classes, meaning a class cannot directly extend more than one class. However, multiple inheritance can be achieved through interfaces. Here’s an example demonstrating multiple inheritance through interfaces:

types of inheritance in java
Example
// Interface with a method
interface Walkable {
    void walk();
}

// Interface with a method
interface Swimmable {
    void swim();
}

// Parent class (superclass)
class Animal {
    void eat() {
        System.out.println("Animal is eating.");
    }

    void sleep() {
        System.out.println("Animal is sleeping.");
    }
}

// Child class (subclass) inheriting from Animal and implementing Walkable and Swimmable interfaces
class Dolphin extends Animal implements Walkable, Swimmable {
    // Implementing the walk() method from the Walkable interface
    @Override
    public void walk() {
        System.out.println("Dolphin is walking on its flippers.");
    }

    // Implementing the swim() method from the Swimmable interface
    @Override
    public void swim() {
        System.out.println("Dolphin is swimming gracefully.");
    }
}

public class Main {
    public static void main(String[] args) {
        // Creating an instance of the Dolphin class
        Dolphin myDolphin = new Dolphin();

        // Accessing methods from Animal, Dolphin, Walkable, and Swimmable
        myDolphin.eat();    // Inherited from Animal
        myDolphin.sleep();  // Inherited from Animal
        myDolphin.walk();   // Implemented from Walkable interface in Dolphin
        myDolphin.swim();   // Implemented from Swimmable interface in Dolphin
    }
}

In this example:

  • Animal is the parent class (superclass) with methods eat() and sleep().
  • Walkable and Swimmable are interfaces with a single method each: walk() and swim().
  • Dolphin is a child class (subclass) that extends Animal and implements both Walkable and Swimmable interfaces.
  • The main method demonstrates creating an instance of the Dolphin class and calling methods from the Animal, Dolphin, Walkable, and Swimmable interfaces.

This illustrates multiple inheritance through interfaces in Java. The Dolphin class inherits from both the Animal class and the Walkable and Swimmable interfaces.

These types of inheritance provide flexibility in designing class hierarchies, and each has its own set of advantages and considerations. Java’s support for single inheritance for classes helps prevent some of the complexities associated with multiple inheritance, while interfaces provide a way to achieve multiple inheritance-like behavior.

Leave a Reply

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