Interfaces
Overview
An Interface is the ultimate contract in Java. While a class dictates how something is done, an Interface strictly dictates what must be done, without providing any of the code.
Because Java prevents Multiple Inheritance (you can only extend one Class), Interfaces were created as the workaround. A single class can implements unlimited interfaces!
Historically, interfaces could only contain 100% abstract methods. However, modern Java (Java 8+) introduced default methods, allowing interfaces to provide fallback logic. Interfaces are widely used to guarantee that completely unrelated classes (like a Car and a Laptop) share a specific capability (like chargeBattery()).
Syntax
// 1. Defining the Contract
public interface Drivable {
// Variables are implicitly 'public static final' (Constants)
int MAX_SPEED = 120;
// Methods are implicitly 'public abstract'
void accelerate();
void brake();
}
// 2. Fulfilling the Contract
// A class can implement multiple interfaces: implements Drivable, Flyable
public class Tesla implements Drivable {
@Override
public void accelerate() {
System.out.println("Accelerating silently...");
}
@Override
public void brake() {
System.out.println("Regenerative braking applied.");
}
}
public class Main {
public static void main(String[] args) {
// Polymorphism works seamlessly with Interfaces!
Drivable myCar = new Tesla();
myCar.accelerate();
}
}Common Pitfalls
- Trying to create instance variables inside an Interface. If you type
int speed;, the compiler instantly upgrades it topublic static final int speed;. It becomes a global constant, NOT an instance variable! You cannot use interfaces to store state. - Forgetting to make implemented methods
public. Methods inside an interface are implicitlypublic. When you override them in your class, you MUST explicitly typepublic. If you forget, it defaults to package-private, which is a reduction in visibility, causing a compilation error.
Interview Questions
A Marker Interface (like Serializable or Cloneable) is an interface that contains absolutely no methods or fields. It simply 'marks' a class, signaling to the JVM or compiler that this class possesses a certain capability and should be treated specially during runtime.
default methods in interfaces (methods with bodies), hasn't that created the Diamond Problem if a class implements two interfaces with the same default method?Yes! If Interface A and Interface B both have a default void start(), and a class implements both, the compiler will crash to prevent ambiguity. The class is forced to manually override start() to resolve the conflict.
Real-World Example
JDBC (Java Database Connectivity) is almost entirely comprised of Interfaces (Connection, Statement, ResultSet). Java provides the interfaces, and database vendors (Oracle, MySQL, Postgres) write the concrete classes that implement them. Your code only ever talks to the Interface, allowing you to swap out your entire database backend without changing a single line of your core logic.
// We only talk to the Interface!
Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM users");Check Your Knowledge
Test your understanding of Interfaces with these quick questions.