Java Blog

 
 
 
interface LOCOMOTIVE
{
    void forward();
    void reverse();
    void stop();
}

interface SOUND
{
   void beep();
}

class ROBOT
{
}

class ROBOTA extends ROBOT
{
}

class ROBOTB extends ROBOT implements LOCOMOTIVE
{
      public void forward()
      { System.out.println(\"ROBOTB.FORWARD()\"); }

      public void reverse()
        { System.out.println(\"ROBOTB.REVERSE()\"); }

      public void stop()
        { System.out.println(\"ROBOTB.STOP()\"); }
}

class ROBOTC extends ROBOT implements LOCOMOTIVE,SOUND
{
      public void forward()
      { System.out.println(\"ROBOTC.FORWARD()\"); }

      public void reverse()
        { System.out.println(\"ROBOTC.REVERSE()\"); }

      public void stop()
        { System.out.println(\"ROBOTC.STOP()\"); }

      public void beep()
      { System.out.println(\"ROBOTC.BEEP()\"); }
}

class ROBOTA1 extends ROBOTA implements SOUND
{
    public void beep()
    {  System.out.println(\"ROBOTA1.BEEP()\"); }
}

class ROBOTB1 extends ROBOTB implements SOUND
{
    public void beep()
    {  System.out.println(\"ROBOTA1.BEEP()\"); }   
}

class ROBOTB2 extends ROBOTB 
{
}

class ROBOTC1 extends ROBOTC
{
}

class  INSTANCEOF_example
{
       public final static int totclass=7;
    public static void main(String args[]) 
    {
    ROBOT  r[] = new ROBOT[totclass];
    r[0]=new ROBOTA();
    r[1]=new ROBOTB();
    r[2]=new ROBOTC();
    r[3]=new ROBOTA1();
    r[4]=new ROBOTB1();
    r[5]=new ROBOTB2();
    r[6]=new ROBOTC1();

          //for BEEP() method
          System.out.println(\"\\nINSTANCEOF SOUND INTERFACE\");
          for(int i=0;i<totclass;i++)
          {    
               if(r[i] instanceof SOUND)
               {
                    r[i].beep();
                }
         }

         System.out.println(\"\\n\\nINSTANCEOF LOCOMOTIVE INTERFACE\");
         for (int i=0;i<totclass ;i++ )
         {
               if(r[i] instanceof LOCOMOTIVE)
               {
                    r[i].stop();
                }
         }
    }
}

 
 
Didn't find what you were looking for? Find more on An application that combines several classes and interface