blog

Home / DeveloperSection / Blogs / Interface in java

Interface in java

Anonymous User 842 06-Sep-2018

Interfaces

An interface is a reference type in java and similar to a class. With in the interface we cannot write any implementation because it has to highlight just the set of services what we are offering and what you are expecting. Hence every method present inside the interface should be abstract.

Advantages of interfaces:

• We can achieve security because we are not highlighting internal implementation.

• Enhancement becomes easy because without affecting outside person we can change our internal implementation.

• Two different systems communicate via interface.

Declaration and implementation of interface:

         We can declare an interface using interface keyword and implement it using keyword implement.

interface intrf{

void m1();
void m2();
}
abstract class service provider implements intrf{
public void m1();
{
}
}

An interface is same written as a class. But a class describes the attributes and behavior of an object. And an interface contains behaviors that a class implements.

If a class implements an interface compulsory we should provide implementation for every method of that interface otherwise we have to declare a class as abstract. Violation leads to compile time error.

Extend vs interface:

1. A class can extend only one class at a time.

2. A class can implement any number of interfaces at a time.

3. A class can extend and can implement any no of interfaces simultaneously.

4. An interface can extend any no of interfaces



Updated 06-Sep-2018
I am a content writter !

Leave Comment

Comments

Liked By