Interface in Java

Java language is having the concept of interface.An interface is something like a normal java class.The difference is it not having any method implementation.All the methods in an interface are by default abstract.An interface cannot be instantiated directly.An interface needs to be implemented by classes. All the methods  of interface needs to be implemented in the implementing classes. A class can me implemented with multiple interfaces.Methods of all interfaces should be there in that class.

A sample interface is shown below.The interface Employee is having two abstract methods.One for calculating salary and other for calculating bonus. All the classes implementing this interface must implement the methods too.

public interface Employee {
public double getSalary();
public double getBonus();

Now consider a class developer implementing employee.The two methods should be there.

public class Developer implements Employee{
public double getSalary() {
return 0;

public double getBonus() {
return 0;

An interface can be extended in another interface. Consider an Interface with  name SharHolder. For stake holder , assume the calculation of salary and bonus is there and in addition dividend also needs to be calculated. Here instead of creating new interface with three abstract methods , we can extend the existing interface .It is shown here.

public interface ShareHolder extends Employee {
public double calculateDividend();

So effectively the new interface has three methods , two methods from interface Employee and one extra method exclusively created for ShareHolder.

Difference between interface and Abstract class

An interface can be identified with keyword Interface Abstract class is like a normal class but an abstract keyword is there.Abstract can contain abstract as well as concrete methods.But interface can contain only abstract methods . In case of abstract class  abstract methods needs to be specified with keyword ‘abstract’, but it is not needed in case of interface.

See Related Topics:

default methods in interfaces