INTERFACE

Interface


  • An interface is a collection of “abstract” methods.
  • An Interface is not a class .writing an interface is similar to writing a class, but they both are two different concepts.
  • A class describes attributes and behavior of an object. An interface contains behaviors that a class implements
  • Unless the class that implements the interface is an abstract, all methods of the interface need to be defined in the class.

  • An Interface is similar to a class in the following ways :

    • An interface can contain any number of methods.
    • You can’t instantiate an interface (or) we can’t create object to interface.
    • An interface doesn’t contain any constructor.
    • All of the methods inside interface are abstract.
    • An interface is not extended by a class, it is implemented by a class.
    • An interface can extend multiple interfaces but implement keyword won’t suit for the interfaces.

    Properties of Interface :

    • An interface is implicitly abstract. You no need to use abstract keyword while declaring an interface.
    • Each method in an interface, is also an implicitly abstract, So the abstract keyword is not required.
    • Methods in an interface are implicitly public.

    Syntax for Interface :

     
                                
                           interface    < interface name >
                           {
                              -----------------;
                              /*   public static final attributes;   */      
                              /*   abstract methods;   */
                           }     
                                

Example 1:

 
						
                    interface A
                    {
                       int m=5;
                       static int j=30;
                       final int i=20;
                    }
                    interface B extends A
                    {
                       public void test();
                    }
                    class D implements B
                    {
                       /* D is not  declared as abstract and does not override  */
                       /* abstract method test() in class D */
                       public static void main(String args[])
                       {
                          System.out.println("Hi interface"); 
                       }
                    }
                    
                       /*  We can make success   by giving  the class name as “abstract” or  */
                     /*  by implementing the abstract method in subclass.  */
                  
                    interface  A
                    {
                       int m=5;
                       static int j=30;
                       final int i=20;
                    }
                    interface B extends A
                    {
                       public void test();
                    }
                    abstract class D implements B
                    {
                       public static void main(String args[])
                       {
                          System.out.println("Hi interface"); 
                       }
                    } 
                        

   Output:

  Hi interface

  • One class can extends only one another class i.e class A extends B
  • One interface can extends many other interfaces i.e interface A extends B,C,D
  • One class can implement multiple interfaces i.e class Test implements A,B,C

Example 2:

 
						
                    interface  A
                    {
                       int i;
                       /*    Compilation fails at line no3. Because  every attribute  */
                       /*   inside interface is implicitly final, so we should assign a  */
                       /*   value while declaring attributes.  */
                       int test();
                    }
                    interface B
                    {
                       int j=10;
                       int test();
                    }
                    abstract  interface  C extends  A,B
                    {
                       void test(); 
                    }
                    /* abstract class D extends C
                    {
                     
                    } */
                      /*    Compilation fails  from line no 15_19.Because  A class can  */
                      /*   implement an interface but can’t extend an interface.  */
                    class E
                    {
                       void test()
                       {
                          System.out.println(“C”);      
                       }
                    }
                    /* class  F  extends  E implements C
                    {
                  
                    } */
                      /*    Compilation fails from line no 27-31 . because of either there  */
                      /*  is no implementation  for      test() inside “class F” as it  */
                      /*  expands “interface C”  or “class F” isn’t declared as an abstract. */
                    abstract  class Y extends E implements C
                    {
                       
                    }
                    class Z extends  E implements C
                    {
                       /*  protected  int test() ;    */
                       /*  Compilation fails at line no 39 Because  test() is an  abstract method  */
                       /*   inside  “interface C” . by default  */
                       /*  evry method  access level is “public” inside interface .While overrding */
                       /*  we must provide same access level or wider access level ,But here “public” */
                       /*  is the wider most access level,so we must use public access level only */
                       /*  while implementing it.We should not use any other narrow  type access  */
                       /*  level like private/default/protected. */
                       {
                          System.out.println(“ABC”);
                       }
                    }