Interface in java

What is an interface

Interface in java is functionality to achieve an object-oriented concept of abstraction. Abstraction means showing the high-level details and hiding the low-level details.

Day-to-day life examples

a ) When we are using a keyboard, we click buttons and it reflects on the screen. Many of us are not aware of how this process works in the background.

b ) We are driving our car daily but what all details do we know about it? We know how to use the steering, how to use brakes, clutch, etc. But we don’t know what happens in the background when we apply a brake?

So these are examples of abstraction, we just know about the top-level details and the underlined details are hidden. Java achieves this functionality by making use of a concept called Interface. The interface is a collection of abstract methods ( methods that do not have any implementation they just have the declarations )

public interface CarInterface 
{
  String manufacturerName();
  String modelName();
  void topSpeed();
}

From the above ” CarInterface ” we came to know that a car is having a manufacturer name, a model name, and a top speed. But we don’t know the underlined details like who is the manufacturer or what is the model name or the top speed.

So to provide the underlined details different Car implementer’s come into the picture. Each implementer implements the above functionality. The class has to implement the interface in java and the method definitions will be provided as part of those classes. We can implement an Interface using the implements keyword.

class BMWCar implements CarInterface 
{
  @Override
  public String manufacturerName() 
  {
    return "Bayerische Motoren Werke";
  }

  @Override
  public String modelName() 
  {
    return "BMW M8";
  }

  @Override
  public void topSpeed() 
  {
    System.out.println("60 mph in 3 seconds");
  }
}

class LamborghiniCar implements CarInterface 
{
  @Override
  public String manufacturerName() 
  {
    return "Automobili Lamborghini";
  }

  @Override
  public String modelName() 
  {
    return "Lamborghini Aventador";
  }

  @Override
  public void topSpeed() 
  {
    System.out.println("0 to 60 in 2.9 secs");
  }
}

There can be many implementers which can provide implementations according to their product specs.

Points to remember

Interface methods are by default abstract and public

abstract: Abstract methods do not have a body. Some Implementer class has to implement those methods.

public: If the methods are not public then we cannot inherit them in other packages or classes. As methods are abstract so compulsory they should have some implementation class else it would be of no use.

Interface variables are by default public, static and final

what type of variable can be defined in an interface?

public, static, and final variables can be defined in an interface

public: So that we can access them across all the packages and the classes.

static: As Interface cannot have a constructor, hence we cannot initialize its variables. Hence there is no way to use those variables unless they are static.

Note: We can initialize the Abstract class variables. As we know abstract class can have a constructor so we can initialize the variables of an abstract class using that constructor. But in the case of interface, we don’t have a constructor so there is no way to initialize those variables. So the variables are always static in the interface.

Example of Abstract class :

abstract class Cars 
{
  int wheels;

  Cars(int wheels) 
  {
    this.wheels = wheels;
  }
}

class BMWCarr extends Cars 
{
  String modelName;

  BMWCarr(int wheels, String modelName) 
  {
    super(wheels);
    this.modelName = modelName;
  }

}

public class Demo 
{
  public static void main(String[] args) 
  {
    BMWCarr bmwCar = new BMWCarr(4, "Bayerische Motoren Werke");
    System.out.println("No. of wheels : " + bmwCar.wheels);
  }
}

Output : No. of wheels : 4

An interface cannot have constructors

A class can implement many interfaces. If a class implements many interfaces which consist of constructors then the implementer class has to initialize all those constructors which is not possible from a single class. Hence the interface cannot have constructors.

public interface CarInterface 
{
  CarInterface() 
  {
  }

  String manufacturerName();
  String modelName();
  void topSpeed();
}

CE : Interfaces cannot have constructors

Variables in the interface are final

We can modify the interface variables in different implementation classes if the variables are not final. Consider the example below :

interface CarInterface 
{
  int wheels = 4;
}

class BMWCar implements CarInterface 
{
  void checkWheels() 
  {
    if (4 == wheels) 
    {
      System.out.println("No. of wheels : " + wheels);
    }
  }
}

class LamborghiniCar implements CarInterface 
{
  void updateWheels() 
  {
    wheels = 6; // CE : The final field CarInterface.wheels cannot be assigned
  }
}

public class Demo1 
{
  public static void main(String[] args) 
  {
    BMWCar bmwCar = new BMWCar();
    bmwCar.checkWheels();
  }
}

Compilation Error : The final field CarInterface.wheels cannot be assigned

A Car can have only 4 wheels that are initialized in the interface, but LamborghiniCar Class tries to update the wheels, hence a compilation error is thrown. So java took care of such scenarios and made the interface variables Final.

A Class can implement more than one interface

This functionality is also called multiple inheritance using interface in java

interface Addition 
{
  int add(int a, int b);
}

interface Subtraction 
{
  int subtract(int a, int b);
}

class Operation implements Addition, Subtraction 
{

  @Override
  public int subtract(int a, int b) 
  {
    return a - b;
  }

  @Override
  public int add(int a, int b) 
  {
    return a + b;
  }
}

public class Demo 
{
  public static void main(String[] args) 
  {
    Operation operation = new Operation();
    System.out.println("Addition is : " + operation.add(10, 5));
    System.out.println("Subtraction is : " + operation.subtract(10, 5));
  }

}
o/p : Addition is : 15
      Subtraction is : 5

Multiple interfaces with the same method name

a ) If multiple interfaces are having same method signature and same return type then it is not an issue, the implementer class has to implement only one method.

interface Addition 
{
  int add(int a, int b);
}

interface Sum 
{
  int add(int a, int b);
}

class Operation implements Addition, Sum 
{
  @Override
  public int add(int a, int b) 
  {
    return a + b;
  }
}

public class Demo 
{
  public static void main(String[] args) 
  {
    Operation operation = new Operation();
    System.out.println("Addition/Sum is : " + operation.add(10, 5));
  }
}

o/p : Addition/Sum is : 15

b ) If Multiple Interfaces are having same method name but different Parameters then also it is not an issue, the class will implement both methods.

interface Addition 
{
  int add(int a, int b);
}

interface Sum 
{
  int add(int a, int b, int c);
}

class Operation implements Addition, Sum 
{
  @Override
  public int add(int a, int b) 
  {
    return a + b;
  }

  @Override
  public int add(int a, int b, int c) 
  {
    return a + b + c;
  }
}

public class Demo 
{
  public static void main(String[] args) 
  {
    Operation operation = new Operation();
    System.out.println("Addition/Sum is : " + operation.add(10, 5));
    System.out.println("Addition/Sum is : " + operation.add(10, 5, 5));

  }
}

o/p : Addition/Sum is : 15
      Addition/Sum is : 20

c ) If the Method name is the same but Parameters and Return types are different then also it is not an issue, Class can implement both methods.

interface Addition 
{
  int add(int a, int b);
}

interface Sum 
{
  void add(int a, int b, int c);
}

class Operation implements Addition, Sum 
{
  @Override
  public int add(int a, int b) 
  {
    return a + b;
  }

  @Override
  public void add(int a, int b, int c) 
  {
    System.out.println("Addition/Sum is : " + (a + b + c));
  }
}

public class Demo 
{
  public static void main(String[] args) 
  {
    Operation operation = new Operation();
    System.out.println("Addition/Sum is : " + operation.add(10, 5));
    operation.add(10, 5, 5);

  }
}

o/p : Addition/Sum is : 15
      Addition/Sum is : 20

d ) The issue arises when the method signature is the same but the return types are different.

interface Addition 
{
  int add(int a, int b);
}

interface Sum 
{
  void add(int a, int b);
}

class Operation implements Addition, Sum 
{
  @Override
  public int add(int a, int b) 
  {
    return a + b;
  }

  @Override
  public void add(int a, int b) 
  {
    System.out.println("Addition/Sum is : " + (a + b + c));
  }
}

CE : Duplicate method add(int, int) in type Operation .

This is all about the interface in java prior to java 8. If you like this article please share it. If you want to add anything extra please comment below and write to me if I have gone wrong anywhere.

Leave a Comment