Skip to main content
Published on

Interfaces in Java

Share:

Introduction

Interfaces are a crucial part of object-oriented programming in Java. They provide a structure that ensures consistency and the implementation of essential methods across various classes.

The Role of Interfaces

In Java, interfaces act as contracts. They establish a set of methods that a class must implement. They have no implementation of their own; they only provide method signatures.

Why Use Interfaces?

  1. Polymorphism: Interfaces promote polymorphism. Any object of a class that implements an interface can be referenced as an object of that interface.
  2. Flexibility: Interfaces make the system extensible. They allow code to evolve without breaking classes that already use existing interfaces.
  3. Safety: They establish a strict contract that the class must follow, ensuring that certain methods are implemented.

Design Patterns and Interfaces

Many design patterns in Java make use of interfaces due to their flexibility. For example, the Adapter pattern uses interfaces to make incompatible interfaces compatible, and the Factory pattern can use interfaces to define a creation method.

Difference Between Interfaces and Abstract Classes

While both provide a means to force classes to follow a given contract, there are fundamental differences:

  • Classes can implement multiple interfaces, but can only inherit from one abstract class.
  • Interfaces have no state variables; abstract classes can.
  • Interface methods are, by default, public and abstract. In abstract classes, you have greater flexibility regarding visibility and implementation.

Illustrating with Code

Interfaces are used to ensure the implementation of certain methods:

package com.caffeinealgorithm.programaremjava;

public class Interfaces {
  public void Run() {
    var car = new Car("Nissan", "Juke", "Front", "Manual", "Diesel", 1461, 175, 110, 25070);
    car.info();

    /*
      Make: Nissan
      Model: Juke
      Engine location: Front
      Transmission: Manual
      Fuel: Diesel
      Displacement: 1461 cc
      Top speed: 175 km/h
      Maximum power: 110 hp
      Price: 25070.00 euros
    */
  }
}

class Car implements ICar {
  private String make, model, engineLocation, transmission, fuel;
  private int displacement, topSpeed, maxPower;
  private double price;

  public Car(String make, String model, String engineLocation, String transmission, String fuel,
             int displacement, int topSpeed, int maxPower, double price) {
    this.make = make;
    this.model = model;
    this.engineLocation = engineLocation;
    this.transmission = transmission;
    this.fuel = fuel;
    this.displacement = displacement;
    this.topSpeed = topSpeed;
    this.maxPower = maxPower;
    this.price = price;
  }

  public void info() {
    System.out.printf("Make: %s\n", make);
    System.out.printf("Model: %s\n", model);
    System.out.printf("Engine location: %s\n", engineLocation);
    System.out.printf("Transmission: %s\n", transmission);
    System.out.printf("Fuel: %s\n", fuel);
    System.out.printf("Displacement: %d cc\n", displacement);
    System.out.printf("Top speed: %d km/h\n", topSpeed);
    System.out.printf("Maximum power: %d hp\n", maxPower);
    System.out.printf("Price: %.2f euros", price);
  }
}

interface ICar {
  void info();
}

In this example, the Car class implements the ICar interface, ensuring the existence of the info() method.

Conclusion

Interfaces in Java are essential tools that offer many advantages in terms of polymorphism, flexibility, and safety. They are fundamental in building extensible and maintainable systems. The ability to use interfaces in conjunction with design patterns makes them an invaluable part of any Java developer's toolkit.

Happy coding!