Java Interfaces
An interface defines a contract that classes can implement. Interfaces are central to abstraction, polymorphism, loose coupling, testing, dependency injection, and many Java APIs.
What is an Interface?
An interface describes capabilities that implementing classes promise to provide. It focuses on what operations are available, while each class decides how those operations work.
Creating an Interface
interface Animal {
void makeSound();
void eat();
}Interface methods are implicitly public abstract unless they are declared as default, static, or private.
Implementing an Interface
class Dog implements Animal {
@Override
public void makeSound() {
System.out.println("Woof!");
}
@Override
public void eat() {
System.out.println("Dog eats.");
}
}The implementing methods must be public because interface methods are public.
Polymorphism Through Interfaces
Animal animal = new Dog();
animal.makeSound();The variable is typed as the interface, while the actual object is a concrete implementation. This lets the same code work with many implementations.
Example: Payment Methods
interface PaymentMethod {
void pay(double amount);
}
class CreditCard implements PaymentMethod {
public void pay(double amount) {
System.out.println(
"Paid by credit card: $" + amount
);
}
}
class PayPal implements PaymentMethod {
public void pay(double amount) {
System.out.println(
"Paid by PayPal: $" + amount
);
}
}void checkout(PaymentMethod method, double total) {
method.pay(total);
}checkout does not need to know whether the payment is handled by a card, PayPal, or another future implementation.
A Class Can Implement Multiple Interfaces
interface Printable {
void print();
}
interface Scannable {
void scan();
}
class OfficeMachine
implements Printable, Scannable {
public void print() { }
public void scan() { }
}This provides multiple inheritance of type, even though Java classes cannot extend multiple classes.
Interfaces Can Extend Interfaces
interface Readable {
void read();
}
interface Editable extends Readable {
void edit();
}Default Methods
Since Java 8, interfaces can provide a default implementation.
interface Greeting {
default void sayHello() {
System.out.println("Hello!");
}
}Default methods help interfaces evolve without forcing every existing implementation to immediately implement a new method.
Static Methods
interface MathTools {
static int square(int x) {
return x * x;
}
}
int result = MathTools.square(5);A static interface method belongs to the interface itself and is called using the interface name.
Private Interface Methods
Modern Java also allows private interface methods, which can share implementation logic between default or static methods.
interface Logger {
default void info(String message) {
write("INFO", message);
}
default void error(String message) {
write("ERROR", message);
}
private void write(String level, String message) {
System.out.println(level + ": " + message);
}
}Interface Fields
Fields declared in an interface are implicitly public static final.
interface Config {
int MAX_RETRIES = 3;
}Interface vs Abstract Class
| Interface | Abstract class |
|---|---|
| A class can implement multiple interfaces. | A class can extend only one class. |
| Best for describing capabilities and contracts. | Useful for sharing state and common implementation. |
| No ordinary instance fields. | Can contain instance fields and constructors. |
| Supports abstract, default, static, and private methods. | Supports abstract and concrete instance methods. |
Default Method Conflicts
If two interfaces provide the same default method, the implementing class must resolve the ambiguity.
interface A {
default void show() {
System.out.println("A");
}
}
interface B {
default void show() {
System.out.println("B");
}
}
class C implements A, B {
@Override
public void show() {
A.super.show();
}
}Why Interfaces Matter
- They reduce coupling between components.
- They make polymorphism explicit.
- They make implementations easier to replace.
- They simplify mocking and testing.
- They are heavily used in collections, JDBC, event systems, and frameworks.
Conclusion
Interfaces define contracts that allow unrelated classes to expose common behavior. Understanding implementation, interface-based polymorphism, multiple interfaces, default methods, static methods, private methods, and abstract-class differences is essential for designing flexible Java programs.