Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

  • interfaces in java is a reference type that defines a set of abstract methods that a class must implement.
  • it specifies what a class must do without dictating how it should do it.
  • features -
    • declared using interface keyword.
    • all methods in an interface are implicitely pubilc and abstract.
    • can contain constants
    • cannot instantiate directly.
    • supports multiple inheritance
interface InterfaceName {
    // Constant
    int CONSTANT = 10;

    // Abstract method
    void methodName();

    // Default method (Java 8+)
    default void defaultMethod() {
        System.out.println("Default implementation");
    } 

    // Static method (Java 8+)
    static void staticMethod() {
        System.out.println("Static method");
    }
}

implementing interfaces

  • a class implements an interface using implements keyword, agreeing to provide implementations for all abstract methods defined in the interface.
  • rules -
    • a class must implement all abstract methods of an interface, or it must be declared abstract.
    • a class can implement multiple interfaces.
    • the implementing class must use public access for interface methods (since interfaces methods are implicitly public)
    • interfaces support polymorphism, an interface reference can point to any implementing class’s object.
interface Movable {
    void move(); // Abstract method
    default void stop() {
        System.out.println("Stopping...");
    }
}

class Car implements Movable {
    @Override
    public void move() {
        System.out.println("Car is moving.");
    }
}

class Bike implements Movable {
    @Override
    public void move() {
        System.out.println("Bike is moving.");
    }
}

class Main {
    public static void main(String[] args) {
        Movable car = new Car();
        Movable bike = new Bike();

        car.move();  // Output: Car is moving.
        car.stop();  // Output: Stopping...
        bike.move(); // Output: Bike is moving.
        bike.stop(); // Output: Stopping...
    }
}

features

  • default methods - methods with a body. can be overriden.
interface Vehicle {
    void drive();
    default void honk() {
        System.out.println("Beep beep!");
    }
}

class Truck implements Vehicle {
    @Override
    public void drive() {
        System.out.println("Truck is driving.");
    }
}

class Main {
    public static void main(String[] args) {
        Truck truck = new Truck();
        truck.drive(); // Output: Truck is driving.
        truck.honk();  // Output: Beep beep!
    }
}
  • static methods - belong to the interface itself not instances
interface Utility {
    static void helper() {
        System.out.println("Static helper method.");
    }
}

class Main {
    public static void main(String[] args) {
        Utility.helper(); // Output: Static helper method.
    }
}
  • constants - fields in an interface are implicitly public, static, final keyword.