Write a program in Java to demonstrate single inheritance, multilevel inheritance and hierarchical inheritance

Single, Multilevel, and Hierarchical Inheritance in Java

Inheritance is a powerful feature in Java that allows you to reuse code and create more complex classes. In this blog post, we will demonstrate single, multilevel, and hierarchical inheritance in Java.

Single Inheritance

In single inheritance, a subclass inherits from a single superclass. This means that the subclass can access all the public and protected members of the superclass.

For example, the following code shows a class called Animal and a subclass called Dog.

class Animal {
    public String name;
    public int age;
}

class Dog extends Animal {
    public String breed;
}

public class Main {
    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.name = "Spot";
        dog.age = 3;
        dog.breed = "German Shepherd";

        System.out.println(dog.name); // Spot
        System.out.println(dog.age); // 3
        System.out.println(dog.breed); // German Shepherd
    }
}

The Dog class inherits the name and age properties from the Animal class. It also has its own breed property.


Multilevel Inheritance

In multilevel inheritance, a subclass can inherit from another subclass. This means that the subclass can access all the public and protected members of its superclass and its superclasses.

For example, the following code shows a class called Animal, a subclass called Dog, and a subclass of Dog called GermanShepherd.

class Animal {
    public String name;
    public int age;
}

class Dog extends Animal {
    public String breed;
}

class GermanShepherd extends Dog {
    public String color;
}

public class Main {
        public static void main(String[] args) {
            GermanShepherd germanShepherd = new GermanShepherd();
            germanShepherd.name = "Lupo";
            germanShepherd.age = 5;
            germanShepherd.breed = "German Shepherd";
            germanShepherd.color = "Black and Tan";

            System.out.println(germanShepherd.name); // Lupo
            System.out.println(germanShepherd.age); // 5
            System.out.println(germanShepherd.breed); // German Shepherd
            System.out.println(germanShepherd.color); // Black and Tan
        }
}


The GermanShepherd class inherits the name, age, breed, and color properties from its superclass, Dog, and its supersuperclass, Animal.


Hierarchical Inheritance

In hierarchical inheritance, multiple subclasses can inherit from a single superclass. This means that the subclasses can share the same properties and methods.

For example, the following code shows a class called Animal, two subclasses of Animal called Dog and Cat, and a subclass of Dog called GermanShepherd.


class Animal {
    public String name;
    public int age;
}

class Dog extends Animal {
    public String breed;
}

class Cat extends Animal {
    public String furColor;
}

class GermanShepherd extends Dog {
    public String color;
}

public class Main {
    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.name = "Spot";
        dog.age = 3;
        dog.breed = "German Shepherd";

        System.out.println(dog.name); // Spot
        System.out.println(dog.age); // 3
        System.out.println(dog.breed); // German Shepherd

        Cat cat = new Cat();
        cat.name = "Garfield";
        cat.age = 7;
        cat.furColor = "Orange";

        System.out.println(cat.name); // Garfield
        System.out.println(cat.age); // 7
        System.out.println(cat.furColor); // Orange
    }
}

The Dog class, Cat class, and GermanShepherd class all inherit the name and age properties from the Animal class.


---------------------------------------------------------------------------------------------------------------------------

I hope this blog post has helped you understand single, multilevel, and hierarchical inheritance in Java. For more information, please refer to the Java documentation: https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html.

---------------------------------------------------------------------------------------------------------------------------

SEO keywords:

  • single inheritance in java
  • multilevel inheritance in java
  • hierarchical inheritance in java
  • java inheritance
  • java inheritance example
  • java inheritance tutorial
  • java inheritance diagram
  • java inheritance blog









Comments