Develop a program for implementation of Single and Multilevel inheritance in java

Single Inheritance

In single inheritance, a class inherits from only one superclass. Let's create a Java program to demonstrate single inheritance.


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

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

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

Output:


This is the parent class.
This is the child class.
        

Multilevel Inheritance

Multilevel inheritance involves a chain of inheritance with more than two levels of classes. Let's create a Java program to demonstrate multilevel inheritance.


class Grandparent {
    void display() {
        System.out.println("This is the grandparent class.");
    }
}

class Parent extends Grandparent {
    void show() {
        System.out.println("This is the parent class.");
    }
}

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

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

Output:


This is the grandparent class.
This is the parent class.
This is the child class.
        

Conclusion

In this tutorial, we developed Java programs to implement single and multilevel inheritance. These concepts are essential in Java for Diploma Engineering students as they provide a foundation for building more complex class hierarchies and software applications.

Comments