Write a Java Program to Implement multiple inheritance

Java supports multiple inheritance through the use of interfaces.

Unlike classes, interfaces cannot implement methods, but can define method signatures which must be implemented by classes that implement the interface.

To implement multiple inheritance in Java using interfaces, we simply create two or more interfaces that define the methods we want to inherit, and then have our class implement all of those interfaces.

Here’s an example Java program that demonstrates multiple inheritance using interfaces:

interface Interface1 {
   public void method1();
}

interface Interface2 {
   public void method2();
}

class MyClass implements Interface1, Interface2 {
   public void method1() {
      System.out.println("method1 called");
   }
   public void method2() {
      System.out.println("method2 called");
   }
}

public class Main {
   public static void main(String args[]) {
      MyClass obj = new MyClass();
      obj.method1();
      obj.method2();
   }
}

In this example, we define two interfaces, Interface1 and Interface2, each with a single method signature.

We then define a class MyClass that implements both interfaces, and provides concrete implementations of each method.

Finally, in our main method, we create an instance of MyClass and call both of its methods.

By implementing multiple interfaces, MyClass is able to inherit the functionality of both Interface1 and Interface2, effectively achieving multiple inheritance.

It’s worth noting that Java only supports multiple inheritance through interfaces, and not through classes.

This is because allowing classes to inherit from multiple classes could lead to complex inheritance hierarchies and potential conflicts between inherited methods and fields.

By limiting multiple inheritance to interfaces, Java provides a cleaner and more predictable approach to achieving multiple inheritance.