Types of Inheritance in Java

In Java, there are four types of inheritance:

1. Single Inheritance:

Single inheritance refers to a class inheriting from only one class. The subclass inherits the properties and methods of the superclass. Here is an example:

class Animal {
    String name;
    public void setName(String name) {
        this.name = name;
    }
}

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

public class Main {
    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.setName("Buddy");
        System.out.println("Dog's name: " + dog.name);
        dog.bark();
    }
}

In the above example, the Dog class inherits from the Animal class using the extends keyword. The Dog class inherits the name property from the Animal class and adds the bark() method.

2. Multilevel Inheritance:

Multilevel inheritance refers to a class inheriting from a superclass that itself inherits from another superclass. Here is an example:

class Animal {
    String name;
    public void setName(String name) {
        this.name = name;
    }
}

class Mammal extends Animal {
    public void speak() {
        System.out.println("I am a mammal");
    }
}

class Dog extends Mammal {
    public void bark() {
        System.out.println("Woof!");
    }
}

public class Main {
    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.setName("Buddy");
        System.out.println("Dog's name: " + dog.name);
        dog.speak();
        dog.bark();
    }
}

In the above example, the Mammal class inherits from the Animal class, and the Dog class inherits from the Mammal class. The Dog class inherits the name property from the Animal class, the speak() method from the Mammal class, and adds the bark() method.

3. Hierarchical Inheritance:

Hierarchical inheritance refers to multiple classes inheriting from the same superclass. Here is an example:

class Animal {
    String name;
    public void setName(String name) {
        this.name = name;
    }
}

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

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

public class Main {
    public static void main(String[] args) {
        Cat cat = new Cat();
        cat.setName("Fluffy");
        System.out.println("Cat's name: " + cat.name);
        cat.meow();

        Dog dog = new Dog();
        dog.setName("Buddy");
        System.out.println("Dog's name: " + dog.name);
        dog.bark();
    }
}

In the above example, both the Cat and Dog classes inherit from the Animal class. The Cat class adds the meow() method, and the Dog class adds the bark() method.

4. Multiple Inheritance through Interfaces:

Java doesn’t support multiple inheritance through classes, but it supports multiple inheritance through interfaces. Here is an example:

interface Flyable {
    void fly();
}

interface Swimmable {
    void swim();
}

class Duck implements Flyable, Swimmable {
    public void fly() {
        System.out.println("Duck is flying");
    }

    public void swim() {
        System.out.println("Duck is swimming");
    }
}

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

In the above example, we have two interfaces Flyable and Swimmable with one method each. The Duck class implements both the Flyable and Swimmable interfaces and provides implementations for their methods.

The Duck class provides implementations for the fly() and swim() methods required by the Flyable and Swimmable interfaces. The Main class creates an instance of the Duck class and calls the fly() and swim() methods, which outputs:

Duck is flying
Duck is swimming

By implementing multiple interfaces, a class can inherit behaviors from multiple sources, allowing it to be more flexible and adaptable.

Wordpress Social Share Plugin powered by Ultimatelysocial
Wordpress Social Share Plugin powered by Ultimatelysocial