Question

The following Java code attempts to demonstrate multiple inheritance, which is not directly supported for classes in Java. How can similar functionality be achieved using interfaces? class A {     void methodA() { System.out.println("Method A"); } } class B {     void methodB() { System.out.println("Method B"); } } // class C extends A, B { } // Problematic: Multiple inheritance not allowed public class Main {     public static void main(String[] args) {         // C obj = new C();     } }

A Change `A` and `B` to interfaces, and `C` to implement both interfaces.
B Use nested classes instead.
C Create a common superclass for `A` and `B`.
D Use composition instead of inheritance.
E Java has no way to achieve this.
Practice Next

Hey! Ask a query