When one interface inherits from another interface, that sub-interface inherits all the methods and constants that its super interface declared. In addition, it can also declare new abstract methods and constants. To extend an interface, you use the extends keyword just as you do in the class definition. Unlike a subclass which can directly extend only one subclass, an interface can directly extend multiple interfaces. This can be done using the following syntax
[public] interface InterfaceName
extends interfacel[, interface2, , interfaceN]
{//interface body}
Here, the name of the interface is InterfaceName. The extends clause declares that this interface extends one or more interfaces. These are known as super interfaces and are listed by name. Each super interface name is separated by commas.
Now let us consider a program that demonstrates how interface extends another interface
interface Interface1
{
public void f1();
}
//Interface2 extending Interface1
interface Interface2 extends Interface1
{
public void f2();
}
class x implements Interface2
{
//definition of method declared in interfacel
public void f1()
{
System.out.println("Contents of Method f1() in Interface1");
}
public void f2()
{
System.out.println("Contents of Method f2() in Interface2");
}
public void f3()
{
System.out.println("Contents of Method f3() of Class X");
}
}
class ExtendingInterface
{
public static void main(String[] args)
{
Interface2 v2; //Reference variable of Interface2
v2 = new x(); //assign object of class x
v2.f1();
v2.f2();
x xl=new x();
xl.f3();
}
}