Interfaces: Contracts for Consistent Behavior
Introduction
An interface defines what a type can do without dictating all implementation details. Classes implement interfaces; one class can implement many interfaces. Combined with Generics, interfaces power List<T>, Map<K,V>, and your own APIs.
Prerequisites
What Is an Interface
public interface Notifier {
void send(String message);
}public class EmailNotifier implements Notifier {
@Override
public void send(String message) {
System.out.println("[Email] " + message);
}
}
public class SmsNotifier implements Notifier {
@Override
public void send(String message) {
System.out.println("[SMS] " + message);
}
}Notifier notifier = new EmailNotifier();
notifier.send("Hello"); // polymorphic call1) Implementing Multiple Interfaces
public interface Auditable {
void audit();
}
public class SecureEmailNotifier implements Notifier, Auditable {
@Override
public void send(String message) {
System.out.println("[Secure Email] " + message);
}
@Override
public void audit() {
System.out.println("Audit log written.");
}
}2) default Methods (Java 8+)
Interfaces can provide default implementations:
public interface Notifier {
void send(String message);
default void sendWithPrefix(String message) {
send("[NOTIFY] " + message);
}
}Subclasses inherit default behavior but may override.
3) static Methods in Interfaces
Utility methods tied to the interface type:
public interface Notifier {
static Notifier console() {
return msg -> System.out.println(msg);
}
}4) Generic Interfaces
public interface Repository<T> {
void save(T item);
T findById(long id);
}List<E> in the JDK is a generic interface—you will use it in Collections.
5) Enums Implementing Interfaces
Enums can implement interfaces:
public enum AlertChannel implements Notifier {
EMAIL {
@Override
public void send(String message) {
System.out.println("Email: " + message);
}
},
SMS {
@Override
public void send(String message) {
System.out.println("SMS: " + message);
}
};
}6) Interface vs Abstract Class
| Choose interface when | Choose abstract class when |
|---|---|
| unrelated types share a role | close family shares code/state |
| multiple contracts needed | single inheritance hierarchy with shared fields |
| API surface for frameworks | template method with shared fields |
Java 8+ interfaces with default methods blur the line—still avoid storing mutable state in interfaces.
7) Functional Interfaces (Preview)
Interface with one abstract method → functional interface → Lambda target (Functional Programming).
@FunctionalInterface
public interface Transformer {
String apply(String input);
}Common Beginner Mistakes
Treating Interface Like a Class
Cannot new Notifier()—implement it or use lambda where functional.
Forgetting implements Keyword
class X implements Y, not extends (except interfaces extending interfaces).
Public Fields in Interfaces
Fields are implicitly public static final constants—rare in modern design.
Mini Practice
Define Drawable with void draw(); — implement in Circle and Square.
What’s Next
Runtime behavior variety: Polymorphism.
FAQ
Can interfaces extend interfaces?
Yes: interface A extends B, C { }
Do interfaces inherit Object?
Implementing classes extend Object; interfaces do not extend classes (except interface extends interface).
Why both abstract class and interface?
Historical and design flexibility. Modern Java favors interfaces for APIs + abstract class for shared base when needed.
Are all Lambdas interfaces?
Lambdas implement functional interfaces at compile time.
What is sealed interface?
Java 17+ restricts permitted implementors—advanced API design.