Interface Extends Interface in Java

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”

interface A {
	void fa();
}

interface B extends A {
	void fb();
}

 

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 another interface java example
 */

interface A {
	void fa();
}

interface B extends A {
	void fb();
}

// Class will implement methods from both interfaces
// As interface B is also extending interface A
class XYZ implements B {

	@Override
	public void fa() {
		System.out.println("XYZ:fa");
	}

	@Override
	public void fb() {
		System.out.println("XYZ:fb");
	}

}

/*
 * Test
 */
public class Sample {

	public static void main(String[] args) {

		XYZ obj = new XYZ();
		obj.fa();
		obj.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.

interface A {
	void fa();
}
interface C {
	void fc();
}

interface B extends A,C {
	void fb();
}

 

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.

/*
 * Interface extends multiple interfaces java example
 */

interface A {
	void fa();
}
interface C {
	void fc();
}

interface B extends A,C {
	void fb();
}


// Class will implement methods from all interfaces
// As interface B is also extending interface A and C
class XYZ implements B {

	@Override
	public void fa() {
		System.out.println("XYZ:fa");
	}

	@Override
	public void fb() {
		System.out.println("XYZ:fb");
	}

	@Override
	public void fc() {
		System.out.println("XYZ:fc");
		
	}

}

/*
 * Test
 */
public class Sample {

	public static void main(String[] args) {

		XYZ obj = new XYZ();
		obj.fa();
		obj.fb();
		obj.fc();

	}

}



Leave a Comment