Interface Extends Interface in Java

(Last Updated On: March 5, 2019)

Learn how an interface extends interface in java with example. Also, example of Interface Extends Multiple Interface in Java program is included.

Interface extends interface in java

An interface can inherit or say, can extends another interface or other multiple interfaces in java programs. We will understand them by code example. but, before that you must be knowing the following.

An interface extends another interface using the keyword “extends”.

Note that, when a class inherit an interface, we use the keyword “implements” like “class X implements A”. When interface inherits another interface, we use “extends” keyword as given in example below.

 

In below example, the interface B is extending another interface A. notice the syntax – “interface B extends A”

 

If a class is implementing the interface B, then the class must implement methods from both the interface B and A as shown in below example.

Example of interface extends another interface in java

In below java code example, the interface B is extending interface A. The class XYZ is implementing only interface B. Note that even though class XYZ is implementing the interface B, the interface A is automatically available to the class as interface B is extending the interface A.

The class has to implements both the methods from interface A and B i.e. fa() and fb().

 

 

Interface Extends Multiple Interface Java

In fact, an interface can extend multiple interfaces like below. Notice the keyword “extends” and a comma symbol in syntax “interface B extends A,C” in below example. The interface B is extending interface A and interface C.

 

Java Interface Extends Multiple Interfaces Example

In this multiple interface extends example, the interface B is extending interfaces A and C. The class XYZ is implementing only class B as “class XYZ implements B”. But, methods from all the interfaces are available to the class. Hence, The class has to implements and define all the methods of all interfaces i.e. A, B and C.