Java Interfaces
In Java, interfaces are one of the most important tools for achieving abstraction and multiple inheritance of type. They allow you to define a contract that classes must follow, without dictating how the methods are implemented.
Interfaces are heavily used in Java frameworks (like Spring, Hibernate) and design patterns to ensure flexibility and reusability.
In this guide, we’ll cover:
- What interfaces are
- Rules for interfaces
- Types of methods in interfaces
- Implementing interfaces with examples
- Best practices
What Is an Interface in Java?
- An interface is a collection of abstract methods, default methods, static methods, and constants.
- A class implements an interface and provides method implementations.
- Interfaces provide a way to enforce contracts in Java.
Rules of Interfaces
- All interface methods are public and abstract by default (except default/static methods).
- Interface variables are public, static, and final by default.
- A class can implement multiple interfaces (unlike classes, which only extend one class).
- Interfaces cannot have constructors.
Methods in Interfaces
1. Abstract Methods
Declared without a body. Classes must implement them.
interface Animal {
void sound(); // abstract method
}
2. Default Methods (Since Java 8)
Provide a default implementation inside the interface. Classes can override them if needed.
interface Animal {
default void sleep() {
System.out.println(“Sleeping…”);
}
}
3. Static Methods (Since Java 8)
Belong to the interface itself, not to objects.
interface Animal {
static void info() {
System.out.println(“All animals make sounds!”);
}
}
4. Private Methods (Since Java 9)
Used for code reusability inside interfaces. Cannot be accessed outside.
interface Helper {
private void log(String msg) {
System.out.println(“Log: ” + msg);
}
default void show() {
log(“Showing details…”);
}
}
Example: Implementing an Interface
// Define interface
interface Drivable {
void accelerate(int increment); // abstract method
default void honk() {
System.out.println(“Beep! Beep!”);
}
}
// Implement interface
public class Car implements Drivable {
private int speed = 0;
@Override
public void accelerate(int increment) {
speed += increment;
System.out.println(“Car speed: ” + speed);
}
public static void main(String[] args) {
Car myCar = new Car();
myCar.accelerate(20); // Car speed: 20
myCar.honk(); // Beep! Beep!
Drivable.info(); // Static method call
}
}
Multiple Interfaces Example
interface Flyable {
void fly();
}
interface Swimmable {
void swim();
}
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”);
}
}
public class TestDuck {
public static void main(String[] args) {
Duck d = new Duck();
d.fly();
d.swim();
}
}
Best Practices
- Use interfaces for contracts, not implementations.
- Prefer default methods for backward compatibility.
- Use static methods for helper utilities.
- Use interfaces over abstract classes when you only need method declarations.
- Design interfaces to be small and focused (Interface Segregation Principle).
Conclusion
Java Interfaces provide a way to achieve abstraction, loose coupling, and multiple inheritance of type.
- Use abstract methods to define contracts.
- Use default methods for optional implementations.
- Use static methods for utilities.
- Implement multiple interfaces to design flexible classes.
By mastering interfaces, you’ll be able to design scalable and reusable Java applications.