Understanding Interface

An interface in Java is a blueprint of a class. It has static constants and abstract methods.The interface in Java is a mechanism to achieve abstraction. There can be only abstract methods in the Java interface, not method body. It is used to achieve abstraction and multiple inheritance in Java.
In other words, you can say that interfaces can have abstract methods and variables. It cannot have a method body.Java Interface also represents the IS-A relationship. It cannot be instantiated just like the abstract class.
Since Java 8, we can have default and static methods in an interface.There are mainly three reasons to use interface. They are given below.
i.It is used to achieve abstraction.
ii.By interface, we can support the functionality of multiple inheritance.
iii.It can be used to achieve loose coupling.

  • How to declare an interface?
  • An interface is declared by using the interface keyword. It provides total abstraction; means all the methods in an interface are declared with the empty body,and all the fields are public, static and final by default. A class that implements an interface must implement all the methods declared in the interface.

    interface {
      // declare constant fields..
      // declare methods that abstract..
      // by default..
    }
  • Multiple inheritance in Java by interface:
  • import java.util.*;
    interface Printable{
      void print();
    }
    interface Showable{
      void show();
    }
    class A7 implements Printable,Showable{
      public void print(){
        System.out.println("PBA");
      }
      public void show(){
        System.out.println("INSTITUTE");
      }
      public static void main(String args[]){
        A7 obj = new A7();
        obj.print();
        obj.show();
      }
    }

    Output:PBA
    INSTITUTE

    Question:Multiple inheritance is not supported through class in java, but it is possible by an interface, why?
    Answer:As we have explained in the inheritance chapter, multiple inheritance is not supported in the case of class because of ambiguity. However, it is supported in case of an interface because there is no ambiguity. It is because its implementation is provided by the implementation class.
    For example:


    import java.util.*;
    interface Printable{
      void print();
    }
    interface Showable{
      void print();
    }
    class TestInterface3 implements Printable, Showable{
      public void print(){
        System.out.println("PBA");
      }
      public static void main(String args[]){
        TestInterface3 obj = new TestInterface3();
        obj.print();
      }
    }
  • Conclusion:
  • In conclusion, interfaces provide a blueprint for classes to implement common methods without specifying the implementation details. They define a set of method signatures that classes must adhere to when implementing the interface. This promotes code reusability, modularity, and flexibility in software design. Interfaces facilitate the creation of loosely coupled systems, enabling easier maintenance and extension of codebases. They are integral to achieving abstraction and polymorphism in Java programming, enhancing code readability and scalability.