Key points about Interfaces in Java

This tutorial explains some of the key points about interfaces which you should be aware about. It will help you to use interfaces correctly. To understand this tutorial easily, you must have knowledge about interfaces in java, if not, refer Interface tutorial first. Following are some key points about interfaces in java :

  • Interfaces cannot be declared as private or protected. Only public and default modifiers are allowed.
  •  private interface A {   }  // Compilation error
     protected interface A {   }  // Compilation error
     public interface A {   }  // Correct
     interface A {   }  // Correct
    
  • Every interface in java is by default abstract.
  •  interface A {   }  // is same as  abstract interface A {   }
    
  • Every variable of an interface is by default public static and final while every method of an interface is by default public and abstract.
  •  interface A {  
        int id = 20;
        void print();
      }
      ......... IS SAME AS ........
      interface A {  
        public static final int id = 20;
        public abstract void print();
      }
    
  • Interface variables must be initialized with some value at the time of declaration.
  •  interface A { 
          // int id;  // Compilation error
          int id = 20;
       } 
    
  • Class implementing an interface cannot change the value of variable declared in the interface since they are declared as final by default.
  •  interface A { 
          int id = 20;
          void test()
      } 
     class Myclass implements A {
        public void test() {
           id = 30; // Compilation error
         }
      }
    
  • Class implementing an interface must implement all the methods of that interface, otherwise the class must be declared as abstract.
  •  interface A { 
          int id = 20;
          void test()
      } 
     abstract class Myclass implements A {
         // Define the class as abstract if not overriding the interface method;
      }
    
  • While implementing the method of an interface in a class, it's visibility(access modifier) can not be reduced. Reducing the visibility will result in compilation error, so it must have public access modifier in class as well.
  •  interface A { 
        void test();
      } 
     class Myclass implements A {
        void test() {  }  // Compilation error
        public void test() {   } // Correct 
      }
    
  • An interface can extend another interface but it cannot implement it. Only classes can implements interfaces.
  •  interface A {   }
     interface B extends A { // valid  } 
     interface B implements A { // Not valid  }
    
  • A class can implement any number of interfaces.
  •  class Myclass implements Interface1, Interface2, Interface3, ....  {
         // Define methods of all implemented interface
      }
    
  • If a class implements two interfaces which have same method name, then the implementation of same name method once is enough.
  •  interface A { 
         int calulateArea();
       }
     interface B { 
         int calulateArea();
       }     
     
     class MyClass implements A, B {
       public int calulateArea() {
          // Defining once is enough.
        } 
     }
  • A class cannot implement two interfaces that have methods with same name but different return type.
  •  interface A { 
         int calulateArea();
       }
     interface B { 
         void calulateArea();
       }     
     
     class MyClass implements A, B {
        // Not possible to define calculateArea() for both interfaces   
     }
  • Variable name conflicts can be resolved by interface name. Conflict is a situation in which a class implements two interfaces which have same variable name. Conflicted variable must be accessed by interface name otherwise compiler will throw error.
  •  interface A { 
         int id = 10;
       }
     interface B { 
         int id = 20;
       }     
     
     class MyClass implements A, B {
       public static void main(String args []) {
          // System.out.println(id); //  compilation error if uncommented
          System.out.println(A.id); // Access interface A variable, prints 10
          System.out.println(B.id); // Access interface B variable, prints 20
        }
     }
  • The object of implementing class can also be assigned into interface type. Using such object you can call only the methods declared inside the interface. If you call a method defined inside class using such object, it will result in compilation error.
  •  interface MyInterface { 
         void print();
       }    
     
     class MyClass implements MyInterface {
        public void print() {
            System.out.println("Inside print method");
         }
        public void message() {
            System.out.println("Inside message method");
         }
        public static void main(String args []) {
          MyInterface obj = new MyClass(); // Assigning MyClass object into MyInterface type
          // obj.message(); // Error if uncommented, message() is not interface method
          obj.print(); // prints "Inside print method"
        }
     }
  • A class can extend another class and can implement an interface as well.
  •  class MyClass extends AnotherClassName implements InterfaceName {  }
  • From java 8 onward an interface can have default and static methods with body. Refer New features in Interfaces tutorial for the same.
  • An interface can have another interface inside it. Refer Nested Interface tutorial for the same.