Interface vs Abstract Class
Before Java 8, an interface had only method signatures, no body — the implementing class had to write everything.
An abstract class can have some complete methods and some abstract (incomplete) ones — and a class can extend only one abstract class, but implement many interfaces.
interface Flyable { void fly(); }
abstract class Bird {
void breathe() { System.out.println("breathing"); }
abstract void fly();
}- Interface = 100% contract, multiple allowed
- Abstract class = partial, only one allowed
- Java 8+ interfaces can have default methods too
A class can implement multiple interfaces (separated by commas) — this is Java's safe way of doing "multiple inheritance of type" (what's not allowed with classes is allowed with interfaces). Like a class implementing both Flyable and Swimmable (say, a Duck class, which can do both).
interface Flyable { void fly(); }
interface Swimmable { void swim(); }
class Duck implements Flyable, Swimmable {
public void fly() { System.out.println("Flying"); }
public void swim() { System.out.println("Swimming"); }
}Before Java 8, an interface only had abstract methods — a 100% contract. Now you can add default methods (with a body) — making it easy to extend existing interfaces with new methods without breaking all their implementing classes (like the sort() method added to the List interface in Java 8, and older classes kept working automatically).
Interfaces can have static methods too, called directly via the interface's name (Interface.method()) — not through the implementing class.
interface Vehicle {
void drive();
default void honk() { System.out.println("Beep!"); } // default body
}An interface with exactly one abstract method is called a "Functional Interface" (default/static methods don't count towards this). These are the target types for Lambda expressions — a core Java 8+ concept (full detail is in the Java 8+ topic).
The @FunctionalInterface annotation is optional but best practice — the compiler checks that the interface truly has only one abstract method, and errors out otherwise (if a second abstract method gets added by mistake).
Some interfaces are completely empty, with no methods at all — like Serializable, Cloneable. They act purely as a "marker/tag", telling the JVM that objects of this class need to be treated in a special way (like Serializable objects being convertible into a byte stream, unlike non-Serializable ones).
This is the most common comparison asked in interviews — their purposes overlap, but there are key differences that decide when to use which.
| Criteria | Interface | Abstract Class |
|---|---|---|
| Methods | Abstract + default/static (Java 8+) | Both abstract + concrete |
| Multiple inheritance | A class can implement many | Can extend only one |
| Constructors | Not allowed | Allowed |
| Fields | Only public static final (constants) | Instance fields of any type |
| When to use | A common contract across unrelated classes | Shared code across related classes |