Wednesday, September 10, 2025

Interesting things with Java Interface

Since Java 8, interfaces can have methods with bodies. Before this update, interfaces could only contain abstract methods, which are method signatures without an implementation. Now, methods in an interface can have bodies if they are declared as default, static, or (since Java 9) private. 

Default methods
Default methods provide a default implementation for a method directly in an interface. Classes that implement the interface can use this default implementation without having to provide their own, or they can choose to override it. 
Purpose: This feature was added primarily for backward compatibility. It allows a developer to add new methods to an existing interface without breaking all the classes that already implement it. 
public interface Vehicle {
    void start(); // Abstract method (no body)

    default void stop() { // Default method with a body
        System.out.println("The vehicle has stopped.");
    }
}

public class Car implements Vehicle {
    @Override
    public void start() {
        System.out.println("The car has started.");
    }
}

public class Main {
    public static void main(String[] args) {
        Car myCar = new Car();
        myCar.start();
        myCar.stop(); // Uses the default implementation from the interface
    }
}

Static methods
Static methods in an interface are associated with the interface itself, not with any specific instance of a class that implements it. They must be called using the interface's name and cannot be overridden by implementing classes. 
Purpose: This is useful for grouping utility methods that are related to the interface. 
public interface Calculator {
    static int add(int a, int b) { // Static method with a body
        return a + b;
    }
}

public class MyProgram {
    public static void main(String[] args) {
        // Call the static method directly on the interface
        int sum = Calculator.add(5, 3);
        System.out.println("Sum: " + sum); // Output: Sum: 8
    }
}

Private methods (Java 9+)
Introduced in Java 9, private methods within an interface are helper methods that can only be called from other methods inside the same interface. They are used to reduce code duplication in default or static methods. 
Purpose: To refactor code shared between default and static methods without exposing the helper method to implementing classes. 
public interface Loggable {
    default void logInfo(String message) {
        log(message, "INFO");
    }

    default void logWarning(String message) {
        log(message, "WARNING");
    }
    
    private void log(String message, String level) { // Private helper method
        System.out.println("[" + level + "] " + message);
    }
}

public class EventLogger implements Loggable {
    // EventLogger only needs to implement the interface; it inherits the default methods.
}

public class Main {
    public static void main(String[] args) {
        EventLogger logger = new EventLogger();
        logger.logInfo("System starting up.");
        logger.logWarning("Connection lost.");
    }
}

References from  Google  
AI Mode

No comments:

Post a Comment

The AI Driven Software Developer, Optimize Innovate Transform

  The AI-Driven Software Developer: Optimize, Innovate, Transform": AI Transformation in Software Development : Understand how AI is re...