Web Analytics

Interfaces in Java

Intermediate ~40 min read

An interface in Java is a reference type that defines a contract - a set of methods that implementing classes must provide. Interfaces enable multiple inheritance of behavior and are fundamental to Java's polymorphism.

What is an Interface?

An interface is like a completely abstract class. It defines what a class can do, not how it does it.

// Define an interface
interface Drawable {
    void draw();  // Abstract by default
}

// Implement the interface
class Circle implements Drawable {
    @Override
    public void draw() {
        System.out.println("Drawing a circle");
    }
}
Key Characteristics:
  • Declared with interface keyword
  • Methods are public abstract by default
  • Variables are public static final by default (constants)
  • Cannot be instantiated
  • Classes use implements keyword

Implementing Interfaces

A class implements an interface using the implements keyword and must provide implementations for ALL abstract methods.

interface Animal {
    void makeSound();
    void move();
}

class Dog implements Animal {
    @Override
    public void makeSound() {
        System.out.println("Woof!");
    }

    @Override
    public void move() {
        System.out.println("Dog runs");
    }
}

Multiple Inheritance via Interfaces

Unlike classes, a class can implement multiple interfaces. This is Java's way of achieving multiple inheritance.

Java Interface Implementation Diagram showing a class implementing multiple interfaces
interface Flyable {
    void fly();
}

interface Swimmable {
    void swim();
}

interface Walkable {
    void walk();
}

// Duck can do all three!
class Duck implements Flyable, Swimmable, Walkable {
    @Override
    public void fly() {
        System.out.println("Duck is flying");
    }

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

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

Default Methods (Java 8+)

Since Java 8, interfaces can have methods with default implementations. This allows adding new methods to interfaces without breaking existing implementations.

interface Vehicle {
    void start();
    void stop();

    // Default method - provides implementation
    default void honk() {
        System.out.println("Beep beep!");
    }
}

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

    @Override
    public void stop() {
        System.out.println("Car stopping");
    }

    // honk() is inherited, but can be overridden
}
Why Default Methods? They allow library designers to add new methods to interfaces without breaking all existing implementations. This was crucial for adding Stream API methods to Collection interface in Java 8.

Static Methods in Interfaces (Java 8+)

Interfaces can also have static methods. These are utility methods related to the interface.

interface MathOperations {
    int calculate(int a, int b);

    // Static utility method
    static int add(int a, int b) {
        return a + b;
    }

    static int multiply(int a, int b) {
        return a * b;
    }
}

// Call static methods using interface name
int sum = MathOperations.add(5, 3);  // 8

Private Methods (Java 9+)

Java 9 added private methods to interfaces, allowing code reuse within default methods.

interface Logger {
    default void logInfo(String message) {
        log("INFO", message);
    }

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

    // Private helper method (Java 9+)
    private void log(String level, String message) {
        System.out.println("[" + level + "] " + message);
    }
}
Output
Click Run to execute your code

Constants in Interfaces

Variables declared in interfaces are implicitly public static final:

interface GameConstants {
    int MAX_PLAYERS = 4;        // public static final
    int MIN_PLAYERS = 2;        // public static final
    String GAME_NAME = "Chess"; // public static final
}

// Access as constants
System.out.println(GameConstants.MAX_PLAYERS);  // 4

Extending Interfaces

Interfaces can extend other interfaces using extends. An interface can extend multiple interfaces.

interface Readable {
    void read();
}

interface Writable {
    void write();
}

// Interface extending multiple interfaces
interface ReadWritable extends Readable, Writable {
    void readWrite();  // Additional method
}

class File implements ReadWritable {
    @Override
    public void read() { System.out.println("Reading"); }

    @Override
    public void write() { System.out.println("Writing"); }

    @Override
    public void readWrite() { System.out.println("Read and Write"); }
}

Functional Interfaces

A functional interface has exactly one abstract method. It can be used with lambda expressions.

@FunctionalInterface
interface Calculator {
    int calculate(int a, int b);  // Single abstract method
}

// Using lambda expression
Calculator add = (a, b) -> a + b;
Calculator multiply = (a, b) -> a * b;

System.out.println(add.calculate(5, 3));      // 8
System.out.println(multiply.calculate(5, 3)); // 15

Common Mistakes

  • Forgetting public on implemented methods: Interface methods are public, so implementations must also be public
  • Trying to instantiate an interface: new Drawable() is invalid (use anonymous classes or lambdas instead)
  • Conflicting default methods: When implementing two interfaces with the same default method, you must override it
  • Adding instance variables: Interfaces can only have constants (static final)

Summary

  • Interfaces define contracts using interface keyword
  • Classes implement interfaces with implements keyword
  • A class can implement multiple interfaces (multiple inheritance)
  • Default methods (Java 8+) provide default implementations
  • Static methods (Java 8+) provide utility methods
  • Variables in interfaces are constants (public static final)
  • Functional interfaces have one abstract method (for lambdas)