Back to Study Material Java

Interfaces in Java

An interface in Java is a reference type that defines a contract: a set of method signatures (and since Java 8, default and static implementations) that an...

Interfaces in Java

Contracts That Any Class Can Sign

An interface in Java is a reference type that defines a contract: a set of method signatures (and since Java 8, default and static implementations) that any class implementing the interface must provide. Interfaces are declared with the interface keyword and implemented by classes with the implements keyword. A single class can implement any number of interfaces, making interfaces the Java solution to the multiple inheritance of behavior.

What an Interface Can Contain Before Java 8, interfaces contained only abstract method declarations and constants (public static final fields). Java 8 added default methods—concrete methods inside an interface that serve as fallback implementations—and static methods. Java 9 added private methods to reduce code duplication among default methods. Java 14 and later allowed sealed interfaces (finalized in Java 17) to restrict which classes may implement them. Despite these additions, an interface cannot have instance fields or constructors.

Implementing Multiple Interfaces A class implements multiple interfaces by listing them with commas after implements. This is valid even if two interfaces define a default method with the same signature; the implementing class must then override that method to resolve the conflict. This design keeps multiple behavior inheritance manageable without the confusion of the diamond problem that affects full multiple class inheritance.

Functional Interfaces and Lambdas An interface with exactly one abstract method is a functional interface. Java 8 allows you to implement a functional interface using a lambda expression instead of writing a full anonymous class. Runnable, Comparator, Callable, and Predicate are all functional interfaces. The @FunctionalInterface annotation asks the compiler to verify that the interface has exactly one abstract method, catching accidental additions of extra methods.

Code Example: interface Greetable { String greet(String name);

default String greetLoud(String name) { return greet(name).toUpperCase(); } }

class FormalGreeter implements Greetable { public String greet(String name) { return "Good day, " + name; } }

Greetable g = new FormalGreeter(); System.out.println(g.greet("Alice")); // Good day, Alice System.out.println(g.greetLoud("Alice")); // GOOD DAY, ALICE

// Lambda for a functional interface Greetable casual = name -> "Hey, " + name; System.out.println(casual.greet("Bob")); // Hey, Bob

Interview Tip: Interviewers frequently ask what happens when two interfaces both declare the same default method and a class implements both. The implementing class must override the method; otherwise, the compiler reports an ambiguity error. Inside the override, you can still invoke a specific interface's version using InterfaceName.super.methodName(). Being able to explain this disambiguation rule demonstrates fluency with Java 8+ interface semantics.