Object-Oriented Programming

Java Interfaces

Learn about interfaces in Java — defining contracts, implementing multiple interfaces, default methods, and functional interfaces.

What is an Interface?

An interface is a contract that defines what a class must do, without specifying how. It provides 100% abstraction and enables multiple inheritance in Java.

java
public interface Drawable {
    void draw();  // Abstract by default
}

public class Circle implements Drawable {
    @Override
    public void draw() {
        System.out.println("Drawing a circle");
    }
}

Interface vs Abstract Class

FeatureInterfaceAbstract Class
MethodsAbstract + default + staticAbstract + concrete
FieldsOnly public static finalAny type
Constructor✗ No✓ Yes
Multiple inheritance✓ Yes (implement many)✗ No (extend one)
Access modifierspublic onlyAny

Implementing Multiple Interfaces

java
public interface Flyable {
    void fly();
}

public interface Swimmable {
    void swim();
}

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

    @Override
    public void swim() {
        System.out.println("Duck is swimming");
    }
}

Default Methods (Java 8+)

Interfaces can have methods with a body using default:

java
public interface Logger {
    void log(String message);

    default void logInfo(String message) {
        log("INFO: " + message);
    }

    default void logError(String message) {
        log("ERROR: " + message);
    }
}

public class ConsoleLogger implements Logger {
    @Override
    public void log(String message) {
        System.out.println("[LOG] " + message);
    }
}

// Usage
ConsoleLogger logger = new ConsoleLogger();
logger.logInfo("Server started");    // [LOG] INFO: Server started
logger.logError("Connection lost");  // [LOG] ERROR: Connection lost

Functional Interfaces (Java 8+)

An interface with exactly one abstract method can be used with lambda expressions:

java
@FunctionalInterface
public interface MathOperation {
    double operate(double a, double b);
}

// Usage with lambda
MathOperation add = (a, b) -> a + b;
MathOperation multiply = (a, b) -> a * b;

System.out.println(add.operate(5, 3));       // 8.0
System.out.println(multiply.operate(5, 3));  // 15.0

Practical Example: Plugin System

java
public interface Plugin {
    String getName();
    void execute();

    default void info() {
        System.out.println("Plugin: " + getName());
    }
}

public class SpellChecker implements Plugin {
    @Override
    public String getName() { return "Spell Checker"; }

    @Override
    public void execute() {
        System.out.println("Checking spelling...");
    }
}

public class WordCounter implements Plugin {
    @Override
    public String getName() { return "Word Counter"; }

    @Override
    public void execute() {
        System.out.println("Counting words...");
    }
}

public class Main {
    public static void main(String[] args) {
        Plugin[] plugins = { new SpellChecker(), new WordCounter() };

        for (Plugin p : plugins) {
            p.info();
            p.execute();
            System.out.println();
        }
    }
}

Summary

  • Interfaces define a contract — what methods a class must implement
  • A class can implement multiple interfaces (Java's answer to multiple inheritance)
  • Default methods provide a body in interfaces (backward-compatible additions)
  • Functional interfaces have one abstract method and work with lambda expressions
  • Use interfaces to define capabilities (Flyable, Serializable, Comparable)
  • Use abstract classes for shared code with enforced behavior