Interface extend Interface Java

We know basically Java does not support multiple inheritance. But supports partially through interfaces. Interfaces were introduced just to support multiple inheritance. This tutorial explains inheritance styles with interfaces, inheritance between interfaces and Interface extend Interface.

One principle in inheritance is after extends there must come only one either concrete class or abstract class. But after implements should come only interfaces of any number.

1st style of Interface extend Interface

Following example illustrates inheritance of interfaces.

interface Test                                 // 1st interface
{
  public abstract void display();
}
interface Hello                                // 2nd interface
{
  public abstract void show();
}
public class Demo implements Test, Hello       // implements two interfaces 
{
  public void display()
  {
    System.out.println("From display() method");
  }
  public void show()
  {
    System.out.println("From show() method");
  }
  public static void main(String args[])
  {
    Demo d1 = new Demo();
    d1.display();
    d1.show();
  }
}

Interface extend Interface JavaOutput Screenshot on Interface extend Interface Java

class Demo implements two interfaces Test and Hello and overrides all the abstract methods of both interfaces. This everyone does and knows. See the second style.

2nd style of Interface extend Interface
interface Test                                 // 1st interface
{
  public abstract void display();
}
interface Hello extends Test                  // 2nd interface extends 1st interface
{
  public abstract void show();
}
public class Demo implements Hello            // implements one interface 
{
  public void display()
  {
    System.out.println("From display() method");
  }
  public void show()
  {
    System.out.println("From show() method");
  }
  public static void main(String args[])
  {
    Demo d1 = new Demo();
    d1.display();
    d1.show();
  }
}

Interface extend Interface JavaOutput Screenshot on Interface extend Interface Java

You get the same output of earlier but style of interfaces implementation is different.

Here class Demo implements Hello interface and inturn Hello interface extends Test interface. That is, between two interfaces use extends keyword; using implements raises compilation error.

To know more on Interface extend Interface, read Play with Implements and Extends Implements.

Practical application of Interface extend Interface comes in RMI (Remote Method Invocation) and EJB (Enterprise Java Beans).

Leave a Comment

Your email address will not be published.