Interface

Interface

An interface in Java is a blueprint of a class. It has static constants and abstract methods.

The interface in Java is a mechanism to achieve abstraction. There can be only abstract methods in the Java interface, not method body. It is used to achieve abstraction and multiple inheritance in Java.

In other words, you can say that interfaces can have abstract methods and variables. It cannot have a method body.

Java Interface also represents the IS-A relationship.

Why use Java interface?


Purpose of Interfaces:

  • To achieve abstraction.
  • To support multiple inheritance since a class can implement multiple interfaces but can only inherit from one class.
  • To specify a contract that implementing classes must follow.

Definition and Syntax:

  • An interface is defined using the interface keyword.
  • Methods in an interface are implicitly abstract and public.
  • Fields in an interface are implicitly public, static, and final.
  •  A class that implements an interface must implement all the methods declared in the interface.

Example in Java:

Here’s a simple example to illustrate interfaces in Java:

Interface Definition:

// Interface
public interface Vehicle {
// Abstract method (implicitly public and abstract)
void startEngine();

// Abstract method
void stopEngine();

// Default method
default void honk() {
System.out.println("Honking the horn!");
}

// Static method
static void displayType() {
System.out.println("This is a vehicle.");
}
}
Notes: Before Java 8, we could only declare abstract methods in an interface. However, Java 8 introduced the concept of default methods. Default methods are methods that can have a body. The most important use of default methods in interfaces is to provide additional functionality to a given type without breaking down the implementing classes.

Implementing the Interface:

/ Class that implements the Vehicle interface
class Car implements Vehicle {

// Providing implementation for the abstract methods
@Override
public void startEngine() {
System.out.println("The car engine starts.");
}

@Override
public void stopEngine() {
System.out.println("The car engine stops.");
}
}
public class Main {
public static void main(String[] args) {
Car car = new Car();
car.startEngine(); // Calls the implemented method
car.stopEngine(); // Calls the implemented method
car.honk(); // Calls the default method from the interface

// Calling the static method from the interface
Vehicle.displayType();
}
}
Notes: Since static methods don't belong to a particular object, they're not part of the API of the classes implementing the interface; therefore, they have to be called by using the interface name preceding the method name. Defining a static method within an interface is identical to defining one in a class.

Multiple Inheritance:

A class can implement multiple interfaces. This is Java's way of providing a form of multiple inheritance, allowing a class to inherit method signatures from more than one interface.

Example of Multiple Inheritance:

interface Flyable {
void fly();
}

interface Swimmable {
void swim();
}

class Duck implements Flyable, Swimmable {
@Override
public void fly() {
System.out.println("The duck flies.");
}

@Override
public void swim () {
System.out.println("The duck swims.");
}
}

public class MultipleInheritance {
public static void main(String[] args) {
Duck duck = new Duck();
duck.fly(); // Calls the method from Flyable interface
duck.swim(); // Calls the method from Swimmable interface
}
}
In this example, the Duck class implements both Flyable and Swimmable interfaces, providing concrete implementations for both fly() and swim() methods. This demonstrates how interfaces enable a form of multiple inheritance in Java.



Comments