Java- Interfaces

Interfaces

  • We can declare interface by ‘interface’ keyword & implementing using ‘implements’ keyword. The allowed modifiers for interface are
    public
    abstract
    strictfp
    <default>
    
  • Whenever a class implementing an interface we should provide the implementation for all the interface methods. Otherwise the class must be declared that class as abstract. Violation leads to compile time error.

  • By default, all interface methods are ‘public abstract’ & variables are ‘public static final’.

  • Whenever we are implementing an interface method compulsory we should declare that method as public, otherwise we will get compile time error.
    interface sample {
    	void m1();
    }
        
    class Test implements sample {
    	void m1() {
    	}
    }
    -=------------------------------------------------------------
    Test.java:13: error: m1() in Test cannot implement m1() in sample
            void m1() {
                 ^
      attempting to assign weaker access privileges; was public
    1 error
    


  • Every interface variable is by default public static and final. Hence the following declarations are equal inside interface.
    int i = 10;
    public int i = 10;
    public static int i = 10;
    public static final int i = 10;
    


  • For interface variables we should perform initialization at the time of Declaration only. because they are final by default. For final variables we must provide value at the time of initialization.
    interface inter
    {
    int i;  C.E = expected.
    }
    


  • interface variables are by default available in the implemented classes. From the implementation classes we are allowed to access but not allowed to change their values i.e reassignment is not possible because these are final.

    interface inter {
    	int i = 10;
    }
        
    class test implements inter {
       public static void main(String arg[]) {	
         i=20;
         System.out.println(inter.i);
      }
    }
    ------------------------------------------
    B.java:7: error: cannot assign a value to final variable i
                    i=20;
                    ^
    


  • we can re-declare interface variable in implemented class with same variable name, there is no Error, because both are created two different memory areas
    interface inter {
      int i = 100;
    }
        
    class Demo implements inter {
      static int i = 200;
      public static void main(String arg[]) {
        System.out.println(i);
        System.out.println(inter.i);
      }
    }
    -------------------------------------------
    E:\Users\Kaveti_S\Desktop\Codes\NotepadExamples>java Demo
    200
    100
    


Naming conflicts in interfaces

If two interfaces contain a method with same signature and same return type in the implementation class, only one method implementation is enough

interface Left {
  void m1();
}

interface Right {
  void m1();
}

class Test implements Left, Right {
  public void m1() {
    System.out.println("method");
  }

  public static void main(String[] args) {
    Left l = new Test();
    l.m1();

    Right r = new Test();
    r.m1();
  }
}
------------------------
method
method


If two interfaces contain a method with same signature but different return type, then we can’t implement those two interfaces simultaneously.

interface Left {
	void m1();
}
interface Right {
	int m1();
}
class Test implements Left, Right {
  public void m1() {
         System.out.println("void");
  }	
  public int m1() {
        System.out.println("void");
  }	 
}
--------------------------------------------
Test.java:10: error: m1() in Test cannot implement m1() in Right
        public void m1() {
                    ^ return type void is not compatible with int


Marker Interface

  • an interface which doesn’t contain any methods, treated as ‘Marker’ interface

  • By implementing an interface if our objects will get some special ability(features), such type of interfaces are called -marker” or -taginterface”.

  • Ex: Serializable, Clonable interfaces are marked for some ability.