Java program to implement multiple inheritance
In Java, multiple inheritance is not directly supported. However, it can be achieved through a technique called interface implementation. Here's an example of how you can implement multiple inheritance using interfaces:
r:ot refetheitroad.cominterface A { public void methodA(); } interface B { public void methodB(); } class MyClass implements A, B { public void methodA() { // implementation for methodA } public void methodB() { // implementation for methodB } }
In the above example, we define two interfaces, A
and B
, each with a single method. Then we define a class MyClass
that implements both interfaces by providing the implementation for each method.
To use the implementation of MyClass
, you can create an object of MyClass
and call its methods like this:
MyClass obj = new MyClass(); obj.methodA(); obj.methodB();
This way, you can achieve multiple inheritance in Java using interface implementation.