What is Interface now exactly!?

Okay, so the Interface is a method in Java that is used to achieve abstraction and multiple inheritance in Java interface we can only declare a method in Interface and it can not have a method body inside the interface.

Why Interface?

Interface has the following uses:

  • In Java, we achieve multiple inheritance with the help of Interface.

  • It is used to achieve 100% abstraction.

  • It solves the problem of loose coupling.

Example for Java Interface:

interface printable{
    void show();
}
class hello implements printable{
    public void show(){ //
        System.out.print("hello bro");
    }
}
public class Main
{
    public static void main(String[] args) {
        hello h1 = new hello();
        h1.show();
    }
}

Multiple Inheritance in Java with the Help of Interface :

interface printable{
    void show();
}
interface showable{
    void print();
}
class hello implements printable,showable{
    public void show(){
        System.out.println("hello bro");
    }
    public void print(){
        System.out.println("show bro");
    }
}
public class Main
{
    public static void main(String[] args) {
        hello h1 = new hello();
        h1.show();
        h1.print();
    }
}

One Interface extends another Interface:

interface Printable{  
void print();  
}  
interface Showable extends Printable{  
void show();  
}  
class TestInterface4 implements Showable{  
public void print(){System.out.println("Hello");}  
public void show(){System.out.println("Welcome");}  

public static void main(String args[]){  
TestInterface4 obj = new TestInterface4();  
obj.print();  
obj.show();  
 }  
}