Wednesday, March 1, 2017

Extending multiple interfaces

An interface can extend multiple interfaces, let’s try:

interface MyMaster1 {

      void showMaster1();
}

interface MyMaster2
{
      void showMaster2();
}

interface MyMaster3 extends MyMaster1, MyMaster2
{
      void showMaster3();
}

public class MyImpl implements MyMaster3
{

      public static void main(String args[])
      {
            MyMaster1 m1 = new MyImpl();
            m1.showMaster1();
            //m1.showMaster2();     Error
            //m1.showMaster3();     Error
           
            MyMaster2 m2 = new MyImpl();
            m2.showMaster2();
           
            MyMaster3 m3 = new MyImpl();
            m3.showMaster3();
           
      }

      @Override
      public void showMaster1() {
            System.out.println("Master 1 executed"); 
      }

      @Override
      public void showMaster2()
      {
            System.out.println("Master 2 executed");
      }

      @Override
      public void showMaster3()
      {
            System.out.println("Master 3 executed");
      }
}

Output:
Master 1 executed
Master 2 executed
Master 3 executed


No comments:

Post a Comment