I know that multiple inheritances between Interfaces is possible, e.g.:
public interface C extends A,B {...} //Where A, B and C are Interfaces
But is it possible to have a regular Class inherit from multiple Interfaces like this:
public class A implements C,D {...} //Where A is a Class and C and D are interfaces
Java does not support "multiple inheritance" (a class can only inherit from one superclass). However, it can be achieved with interfaces, because the class can implement multiple interfaces. Note: To implement multiple interfaces, separate them with a comma (see example below).
Simply put, in Java, a class can inherit another class and multiple interfaces, while an interface can inherit other interfaces.
Java allows a class to implement multiple interfaces. So, we can implement as much as we want. In this example, we created 3 interfaces and then implemented them by using a class. While working with the interface, make sure the class implements all its abstract methods.
A Java class can only extend one parent class. Multiple inheritance (extends
) is not allowed. Interfaces are not classes, however, and a class can implement more than one interface.
The parent interfaces are declared in a comma-separated list, after the implements
keyword.
In conclusion, yes, it is possible to do:
public class A implements C,D {...}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With