Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a normal Class implement multiple interfaces?

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 
like image 242
Joshua Avatar asked Jan 21 '14 16:01

Joshua


People also ask

Can one class implement multiple 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).

Can a class inherit multiple interfaces?

Simply put, in Java, a class can inherit another class and multiple interfaces, while an interface can inherit other interfaces.

Can Java have multiple 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.


1 Answers

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 {...} 
like image 184
Christian Tapia Avatar answered Sep 27 '22 22:09

Christian Tapia