What is Interface?

Interface is used to keep incomplete method. Here method should not be incomplete otherwise it will show an error.

Interfaces are 100% abstract or 100% incomplete. In this, Object does not be created. Hence, we does use or create Constructor and IIB methods here.

For Example:

interface A{

public void test(); // method is incomplete.

}

class B implements A{

public void test(){ //method is completed in class

System.out.println(“From Interface”);

}

public void main(){

B b = new B();

b.test();

}

Output: From Interface

In above example we create one class B. That class b has contract with interface A to complete a method and give output otherwise it will put an error. Due to this, we inherit the method in class and do overriding. There must an overriding in interface and class otherwise this type of programs cannot be executed.

Here, access specifier of method is by default Public. And every variable declare in interface is by default public, final and static. As we does not make object, variable cannot be non-static. Every method in interface is by default abstract.

For Example:

interface A{

int i=10; // it is by default public, final and static

}

class B implements A{

public static void main(){

System.out.println(A.i);

}

}

Output: 10

Edusera
Logo
Open chat
1
Scan the code
Hello!👋
Can we help you?