When to use an interface?

In Java, an interface is a complex topic for beginners. Everyone knows we use it to achieve the abstraction. But we should need to recognize the scenario when to use it. We have seen many simple programs in which we are using the interface.  But when we are working on a large project then we should need to understand the advantage of the interface. In this article, we will learn some important points of the interface that help you to understand the when to use interface in java. I will recommend you should read When to use an abstract class first. So that you can find the difference in both and use them accordingly.  

The first use of Interface is to achieve multiple inheritances. As you already know Java doesn’t support the multiple inheritances by use of classes. But it makes possible through the interface.

As you know the interface is just a blueprint or template in which we define the set of functionalities as a rule or a contract. When any concrete class implements an interface, it means it agreed upon the contract of interface. The concrete class provides the implementation of all methods of an interface.

Suppose we have a company of Automobile. We want to manufacture new models of cars with some specific features.  A car must contain features shown below :

1. Automatic gear system
2. Model name
3. Hight tech AC

These are the rule or contract that define by us for each car. Whenever a car is manufacturing, the rules must be considered. Suppose there are two plants that manufacturing the car both should take care of the contract otherwise the process is considered unsuccessful.

when to use interface in java

Let’s achieve this task through the use of an interface. We will create an interface that defines the set of rules or contract. This interface is forcibly implemented by each class that wants to build a car. When a class implements an interface, it must provide the implementation to the abstract methods.

interface NewCarModel 
{
  public void isAutomatic();
  public void modelName();
  public void highTechAC();
}

class FirstPlant implements NewCarModel
{
	
	Car car = new Car("BMW", true, "X5.0", 5.1f, "White", "Petrol", "260bhp", 5);
	
	public void CarFeatures()
	{
		System.out.println("Brand Name : "+ car.getCompany());
		System.out.println("Color of Car : "+ car.getColor());
		System.out.println("Power : "+ car.getPower());
		System.out.println("Fuel type : "+ car.getFuelType());
		System.out.println("No of Gears: " + car.getNumberOfGear());
	}
	
	@Override
	public void isAutomatic() 
	{
		System.out.println("Is this Car automatic : " + car.isAutomatic());
	}

	@Override
	public void modelName() 
	{
		System.out.println("Model name : " + car.getModelName());
	}

	@Override
	public void highTechAC() 
	{
		System.out.println("Model name : " + car.getHighTechAC());
	}
}

class SecondPlant implements NewCarModel
{
	Car car = new Car("AUDI", true, "A5.0", 5.2f, "Black", "Disel", "280bhp", 6);

	public void CarFeatures()
	{
		System.out.println("Brand Name : "+ car.getCompany());
		System.out.println("Color of Car : "+ car.getColor());
		System.out.println("Power : "+ car.getPower());
		System.out.println("Fuel type : "+ car.getFuelType());
		System.out.println("No of Gears" + car.getNumberOfGear());
	}
	
	@Override
	public void isAutomatic() 
	{
		System.out.println("Is this Car automatic : " + car.isAutomatic());
	}

	@Override
	public void modelName() 
	{
		System.out.println("Model name : " + car.getModelName());
	}

	@Override
	public void highTechAC() 
	{
		System.out.println("Model name : " + car.getHighTechAC());
	}

}

public class MainClass
{
  public static void main(String arg[])
  {
	  System.out.println("Car manufacture in first plants");
	  FirstPlant firstPlant = new FirstPlant();
	  firstPlant.CarFeatures();
	  firstPlant.isAutomatic();
	  firstPlant.highTechAC();
	  firstPlant.modelName();
	  
	  System.out.println("Car manufacture in Second plants");
	  SecondPlant secondPlant = new SecondPlant();
	  secondPlant.CarFeatures();
	  secondPlant.isAutomatic();
	  secondPlant.highTechAC();
	  secondPlant.modelName();
  }
}

class Car
{
	String company;
	boolean isAutomatic;
	String modelName;
	float highTechAC;
	String color;
	String fuelType;
	String Power;
	int numberOfGear;
	public String getCompany() {
		return company;
	}
	public void setCompany(String company) {
		this.company = company;
	}
	public boolean isAutomatic() {
		return isAutomatic;
	}
	public void setAutomatic(boolean isAutomatic) {
		this.isAutomatic = isAutomatic;
	}
	public String getModelName() {
		return modelName;
	}
	public void setModelName(String modelName) {
		this.modelName = modelName;
	}
	public float getHighTechAC() {
		return highTechAC;
	}
	public void setHighTechAC(float highTechAC) {
		this.highTechAC = highTechAC;
	}
	public String getColor() {
		return color;
	}
	public void setColor(String color) {
		this.color = color;
	}
	public String getFuelType() {
		return fuelType;
	}
	public void setFuelType(String fuelType) {
		this.fuelType = fuelType;
	}
	public String getPower() {
		return Power;
	}
	public void setPower(String power) {
		Power = power;
	}
	public int getNumberOfGear() {
		return numberOfGear;
	}
	public void setNumberOfGear(int numberOfGear) {
		this.numberOfGear = numberOfGear;
	}
	public Car(String company, boolean isAutomatic, String modelName, float highTechAC, String color, String fuelType,
			String power, int numberOfGear) {
		this.company = company;
		this.isAutomatic = isAutomatic;
		this.modelName = modelName;
		this.highTechAC = highTechAC;
		this.color = color;
		this.fuelType = fuelType;
		Power = power;
		this.numberOfGear = numberOfGear;
	}
}

Output: Car manufacture in first plants
Brand Name : BMW
Color of Car : White
Power : 260bhp
Fuel type : Petrol
No of Gears : 5
Is this Car automatic : true
Model name : 5.1
Model name : X5.0
Car manufacture in Second plants
Brand Name : AUDI
Color of Car : Black
Power : 280bhp
Fuel type : Diesel
No of Gears: 6
Is this Car automatic : true
Model name : 5.2
Model name : A5.0

Here we have created an interface NewCarModel that has three methods isAutomatic(),modelName() and  highTechAC(). These are the abstract method so implementation will provide by the concrete class that implements it. Here we also create two concrete classes FirstPlant and SecondPlant that overriding the abstract method and gives the details of its own implementation.

Leave a Comment