Learn Interface In Java

Sharing is healthy !!

Today we are going to learn interface in java.

In this post we will discuss what is an interface in java with an example.

Learn Interface In Java: what is an interface?

An interface is a contract between class and the outside world. The outside world communicates with the implementing class using this contract.

Let’s take an example and understand this concept

Learn Interface In Java

Vehicle is an interface and it defines the contract that includes operations accelerate, brake and steer.

Driver drives a vehicle using above methods defined in the vehicle contract.

The vehicle contract is implemented by BMW car. So BMW class has to implement the methods accelerate, brake and steer. So,

What are the advantages of interface?

It separates the user of the interface from the implementer of the interface.

In my example what happens if you want driver to drive a Audi instead of BMW?!

Also Read – Learn Abstraction In Java

Simple. We create a new class Audi which implements the vehicle interface.

The changes in driver are very minimal. It is this reason we are encouraged to use the interface instead of a class directly. Let’s see how the code works

public interface Vehicle
{
public void accelerate();
public void brake(); 
public void steer();
}
public class BMW implements Vehicle
{
@Override
public void accelerate()
{
System.out.println("BMW car is accelerating!!"); 
}
@Override 
public void brake() 
{
// TODO Auto-generated method stub
}
@Override 
public void steer()
{
// TODO Auto-generated method stub
}
}
public class InterfaceExample
{ 
{
public static void main(String[] args)
{
Vehicle ve = new BMW();
ve.accelerate();
}
}

Output:

BMW car is accelerating!!

We have defined the vehicle interface and we have declared the method accelerate.

Note that the method does not have a body and semicolon is placed immediately after the method name.

The contract does not include the implementation. It just includes the rules or the declaration.

The same goes with brake and steer method.

Now class BMW implements interface vehicle. In the above example we can see the empty implementation of all the methods declared in vehicle interface.

As we know BMW car should implement all the methods declared in vehicle interface.

I’m printing “BMW car is accelerating” to accelerate method. I’ll will not change other methods for now.

In main method we will create like this,

Vehicle ve = new BMW();

Note that the type is vehicle which is an interface and the class is BMW.

ve.accelerate();

Above statement will invoke accelerate method in BMW. When we run the above example it prints “BMW car is accelerating!!”.

One of the advantages of interface is that the implementation class can change with minimal changes to objects that use them.

Learn Interface In Java: Advantages

  • Interface defines a contract and decouples the class and its users.
  • Separation of concerns; interface for one to think of implementing a separate class for each concern.
  •  When publishing an API, publish only the contract. Do not publish how it is implemented.
  • Interfaces makes it easier to use polymorphism.
conclusion:

We learnt that interface is a contract, discussed example using vehicle interface, two implementations of it and advantages.

I hope you have understood the concept.

Sharing is healthy !!