Thursday, 4 June 2015

Java extending an Interface

One interface can inherit another by use of keyword extends. The syntax is the same as for inheriting classes. When a class implements an interface that inherits another interfaces, it must provide implementations for all methods defined within the interface inheritance chain.

Example:

interface A

  void m1();
  void m2();
}

interface B extends A //interface can be extended
{
  void m3();
}

class MyClass implements B
{
   public void m1()
  {
    System.out.println("m1");
  }

public void m2()
{
   System.out.println("m2");
}

public void m3()
{
   System.out.println("m3");
}

}

class InterfaceExtendDemo
{

public static void main(String args[])

{

MyClass ob=new MyClass();

ob.m1();

ob.m2();

ob.m3();

}

}

Output:
m1
m2
m3

No comments:

Post a Comment