Skip to main content
Documents
Share
LinkedIn
Facebook
Twitter
Copy
Java keywords

interface Keyword in Java

The interface keyword in Java is used to declare a special type of class that only contains abstract methods, default methods, static methods, and final variables. Interfaces provide a way to achieve abstraction and multiple inheritance in Java.

Usage

Interfaces are used to specify a set of methods that a class must implement. They are particularly useful for defining a contract that multiple classes can adhere to, ensuring that they provide specific functionalities.

Syntax

interface InterfaceName {
    // constant fields
    // abstract methods
    // default methods
    // static methods
}
  • InterfaceName: The name of the interface.
  • Constant fields: Fields declared in an interface are implicitly public, static, and final.
  • Abstract methods: Methods without a body that must be implemented by the classes that implement the interface.
  • Default methods: Methods with a body that provide a default implementation.
  • Static methods: Methods with a body that belong to the interface class itself.

Examples

Example 1: Basic Interface

interface Animal {
    void eat();
    void sleep();
}

class Dog implements Animal {
    public void eat() {
        System.out.println("Dog is eating");
    }

    public void sleep() {
        System.out.println("Dog is sleeping");
    }
}

public class InterfaceExample {
    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.eat();
        dog.sleep();
    }
}

In this example, the Animal interface declares two abstract methods: eat and sleep. The Dog class implements the Animal interface and provides concrete implementations for these methods.

Example 2: Interface with Default Method

interface Vehicle {
    void start();
    default void stop() {
        System.out.println("Vehicle is stopping");
    }
}

class Car implements Vehicle {
    public void start() {
        System.out.println("Car is starting");
    }
}

public class DefaultMethodExample {
    public static void main(String[] args) {
        Car car = new Car();
        car.start();
        car.stop();
    }
}

Here, the Vehicle interface includes a default method stop. The Car class implements the Vehicle interface and provides an implementation for the start method. The default method stop is inherited and can be used as is.

Example 3: Interface with Static Method

interface Calculator {
    static int add(int a, int b) {
        return a + b;
    }
}

public class StaticMethodExample {
    public static void main(String[] args) {
        int result = Calculator.add(5, 3);
        System.out.println("Sum: " + result);
    }
}

This example demonstrates an interface Calculator with a static method add. The static method can be called directly using the interface name without needing an instance.

Tips and Best Practices

  • Single Responsibility: Keep interfaces focused on a single responsibility. This makes them easier to understand and implement.
  • Naming Conventions: Use meaningful names for interfaces. Often, interface names are adjectives or nouns that describe the behavior or role, like Runnable, Serializable, or List.
  • Avoid Overloading: Avoid overloading methods in interfaces to maintain clarity and simplicity.
  • Use Default Methods Wisely: Default methods can be useful for providing backward compatibility, but use them sparingly to avoid complicating the interface.
  • Document Interfaces: Clearly document the purpose and expected behavior of each method in the interface to guide implementers.
  • Prefer Composition Over Inheritance: Use interfaces to define capabilities and prefer composition over inheritance to achieve flexible and maintainable code.

Additional Concepts

Multiple Interfaces

A class in Java can implement multiple interfaces, allowing it to inherit the abstract methods of all implemented interfaces.

interface Movable {
    void move();
}

interface Stoppable {
    void stop();
}

class Robot implements Movable, Stoppable {
    public void move() {
        System.out.println("Robot is moving");
    }

    public void stop() {
        System.out.println("Robot has stopped");
    }
}

public class MultipleInterfacesExample {
    public static void main(String[] args) {
        Robot robot = new Robot();
        robot.move();
        robot.stop();
    }
}

In this example, the Robot class implements both Movable and Stoppable interfaces, providing implementations for the methods defined in both interfaces.