201k views
3 votes
Modify the Java program below to demonstrate Hybrid Inheritance:

class C {
public void Print() {
System.out.println("C is the Parent Class to all A,B,D");
}
}
class A extends C {
public void Print() {
System.out.println("A has Single Inheritance with C and shares Hierarchy with B");
}
}
class B extends C {
public void Print() {
System.out.println("B has Single Inheritance with C and shares Hierarchy with A");
}
}
public class D extends A {
public void Print() {
System.out.println("D has Single Inheritance with A and Multi-Level inheritance with C");
}
public static void main(String args[]) {
A w = new A();
B x = new B();
C y = new C();
D z = new D();
y.Print();
w.Print();
x.Print();
z.Print();
}
}
Sample Output:
Demonstration of Hybrid Inheritance:
C is the Parent Class to all A, B, Lab08_C
A has Single Inheritance with C and shares Hierarchy with B
B has Single Inheritance with C and shares Hierarchy with A
Lab08_C has Single Inheritance with A and Multi-Level inheritance with C

1 Answer

2 votes

Final answer:

Hybrid inheritance is a combination of multiple inheritance and multilevel inheritance in object-oriented programming.

Step-by-step explanation:

Hybrid inheritance is a combination of multiple inheritance and multilevel inheritance in object-oriented programming. In the provided Java program, the classes A, B, and D are subclasses of class C. Class A and class B both inherit from class C, representing single inheritance, while class D inherits from class A, demonstrating multilevel inheritance. This combination of different types of inheritance results in hybrid inheritance.

User JBone
by
8.5k points