While Abstract Classes provide a partially implemented blueprint (with both abstract and concrete methods), Interfaces define a purely behavioral contract with zero implementation. Understanding when to use each is a critical design decision in professional OOP development.
An Interface in Java is a reference type, similar to a class, that can contain only abstract methods (prior to Java 8) and constants. It specifies what a class must do, but not how it does it.
A class that implements an interface must provide concrete implementations for ALL methods declared in the interface.
interface Flyable {
void takeOff();
void fly(int altitude);
void land();
}
class Airplane implements Flyable {
public void takeOff() { System.out.println("Runway acceleration"); }
public void fly(int altitude) { System.out.println("Cruising at " + altitude + " feet"); }
public void land() { System.out.println("Landing gear deployed"); }
}
class Bird implements Flyable {
public void takeOff() { System.out.println("Flapping wings"); }
public void fly(int altitude) { System.out.println("Soaring at " + altitude + " feet"); }
public void land() { System.out.println("Perching on branch"); }
}
Both Airplane and Bird are completely unrelated classes, but they both fulfill the Flyable contract.
Starting with Java 8, interfaces gained new capabilities:
default keyword). This allows adding new methods to existing interfaces without breaking all implementing classes.interface Loggable {
void log(String message);
// Default method: implementing classes get this for free
default void logError(String message) {
log("ERROR: " + message);
}
}
Recall that Java bans multiple inheritance of classes because of the Diamond Problem. However, Java allows a class to implement multiple interfaces simultaneously. This provides the benefits of multiple inheritance without the ambiguity.
interface Swimmable {
void swim();
}
interface Flyable {
void fly();
}
// A Duck can both swim AND fly
class Duck implements Swimmable, Flyable {
public void swim() { System.out.println("Paddling"); }
public void fly() { System.out.println("Flapping"); }
}
| Feature | Abstract Class | Interface |
|---|---|---|
| Methods | Can have abstract AND concrete methods | All methods are abstract (pre-Java 8) |
| Variables | Can have instance variables of any type | Can only have public static final constants |
| Constructors | Can have constructors | Cannot have constructors |
| Inheritance | A class can extend only ONE abstract class | A class can implement MULTIPLE interfaces |
| Access Modifiers | Methods can have any access modifier | Methods are implicitly public |
| Use Case | "IS-A" relationship (Dog IS-A Animal) | "CAN-DO" relationship (Duck CAN Fly, CAN Swim) |
Serializable, Comparable, Runnable).