Interfaces in Java

    Interfaces in Java

    Interfaces are considered to be the blueprint of a class. You can declare static constants and abstract method within an interface. With the help of an interface, you can achieve abstraction. You can define abstract methods within the interface but do not define their body. You can also implement the concept of multiple inheritances using interfaces.

    Like inheritance, interfaces also specify the IS-A relationship.  You cannot create the instance of the interface. In the latest Java version, we can have defaulted, static and private methods declared within the interface.

    Features of interface-

    You can achieve abstraction and multiple inheritances.

    You can also achieve loose coupling.

    Declaring interface-

    Declaring interface means that all the methods within the interface have the only declaration but not the body. All the declared fields are public, static and final by default. If you are implementing the class then you have to implement each and every method that is declared within the interface.

    If you do not mention the public, static and final keyword before the fields of the interface then it will be automatically added at the compile time by the compiler.

    Relation among class and interface-

    A class can extend another class but implements the interface. An interface can extend another interface.

    Example-

    interface demo{
    
    void display();}
    
    
    
    class Simple implements demo{
    
    
    
    public void display(){
    
    System.out.println("welcome");}
    
      public static void main(String args[]){  
    
        Simple d= new Simple();  
    
        d.display();
    
      }  
    
    } 

    Output -

    welcome

    Implementing multiple inheritances-

    A class can implement multiple interfaces and an interface can extend multiply interfaces.

    Example-

    interface demo{
    
    void display();}
    
    interface blank{
    
    void show();}
    
    class Simple implements demo, blank{
    
    public void display(){
    
    System.out.println("welcome display");}
    
    public void show()
    
    {
    
    System.out.println("welcome show");}
    
      public static void main(String args[]){  
    
        Simple d= new Simple();  
    
        d.display();
    
    d.show();
    
      }  
    
    } 

    Output-

    welcome display
    welcome show

    You cannot represent multiple inheritances with class because it shows ambiguity. But it is possible with interfaces because the implementation is given by the implementing class.

    Example of Inheritance using interface-

    An interface can extend another interface.

    interface demo{
    void display();}
    
    interface blank extends demo{
    
    void show();}
    
    
    class Simple implements blank{
    
    public void display(){
    
    System.out.println("welcome display");}
    
    public void show()
    
    {
    
    System.out.println("welcome show");}
    
      public static void main(String args[]){  
    
        Simple d= new Simple();  
    
        d.display();
    
    d.show();
    
      }  
    } 

    Output-

    welcome display
    welcome show

    Using the default method with the interface-

    If you want the method body within the interface then you should declare it as the default method (Java 8).

    interface demo{
    
    void display();
    
    default draw(){
    
    System.out.println("welcome default draw");
    
    }
    
    }
    
    
    class Simple implements demo{
    
    
    public void display(){
    
    System.out.println("welcome display");}
    
    public static void main(String args[]){  
    
        Simple d= new Simple();  
    
        d.display();
    
    d.draw();
      }  
    } 

    Output-

    welcome display
    welcome default draw

    Nested interface

    You can declare an interface within another interface.

    interface demo{
    
    void display();
    
    interface mask{ //nested interface
    void cover();
    
    }
    }
    
    class Simple implements demo{
    
    public void display(){
    
    System.out.println("welcome display");}
    
    public void cover(){
    
    System.out.println("welcome cover");}
    
    public static void main(String args[]){  
    
        Simple d= new Simple();  
    
        d.display();
    
    d.cover();
      }  
    } 

    Output-

    welcome display
    welcome cover