This is another core Java interview favorite, so let me break down the differences between an interface and an abstract class in a way that’s easy to remember — and with some practical tips on when to use which.
💥 Interface vs Abstract Class — The Key Differences
| Feature | Interface | Abstract Class |
|---|---|---|
| Purpose | Defines a contract (what a class must do) | Provides a partial implementation (how something works) |
| Methods | Only abstract methods (until Java 8+) — but can have default & static methods now | Can have both abstract and concrete methods |
| Fields | Only public static final constants (no normal fields) | Can have instance fields (normal class variables) |
| Inheritance | A class can implement multiple interfaces (multiple inheritance of type) | A class can extend only one abstract class (single inheritance) |
| Constructor | No constructor (interfaces can’t be instantiated) | Can have a constructor (runs when subclass is created) |
| Access Modifiers | Methods are public by default | Methods can have any access modifier (public, protected, package-private, private) |
| Type of Relationship | Defines what a class can do (capability) | Defines what a class is (inheritance) |
| Speed Evolution | Harder to evolve (changing an interface breaks all implementations) | Easier to evolve (you can add new non-abstract methods without breaking subclasses) |
📚 Example — Interface
public interface Animal {
void makeSound(); // By default: public and abstract
}
📚 Example — Abstract Class
public abstract class Animal {
private String name; // Can have fields
public Animal(String name) {
this.name = name;
}
abstract void makeSound(); // Must be implemented by subclasses
void eat() { // Concrete method (already implemented)
System.out.println(name + " is eating");
}
}
🔗 Key Rule — Inheritance vs Capability
| Concept | Meaning |
|---|---|
| Abstract Class | “This is a base class for other classes to inherit from” (is-a relationship) |
| Interface | “This defines a capability that any class can have” (can-do relationship) |
⚡ Quick Example — Flying Animal
Abstract Class (Base type: Animal)
abstract class Animal {
abstract void eat();
}
Interface (Extra capability: Flyable)
interface Flyable {
void fly();
}
A bird can inherit Animal and implement Flyable:
class Bird extends Animal implements Flyable {
@Override
void eat() {
System.out.println("Bird is eating");
}
@Override
public void fly() {
System.out.println("Bird is flying");
}
}
✅ This shows:
extendsfor abstract class.implementsfor interface.
🔥 Modern Twist (Java 8+)
Java 8+ added:
- default methods in interfaces (interfaces can now have concrete methods).
- static methods in interfaces.
interface Animal {
void makeSound();
default void breathe() { // Concrete method in interface
System.out.println("Breathing...");
}
}
⚠️ This makes interfaces look a bit like abstract classes, but interfaces still cannot have fields (except constants) or constructors.
🏎️ When to Use What?
| Use Case | Use |
|---|---|
| Shared behavior across related classes | Abstract Class |
Unrelated classes need the same ability (like Serializable, Comparable) | Interface |
| You need multiple inheritance of types | Interface |
| You need to define a base class with fields and helper methods | Abstract Class |
🔥 Summary Table
| Feature | Interface | Abstract Class |
|---|---|---|
| Fields | Only public static final | Any type (private, protected, etc.) |
| Multiple Inheritance | ✔️ Yes (can implement many interfaces) | ❌ No (can extend only one abstract class) |
| Concrete Methods | ✔️ From Java 8+ (default, static) | ✔️ Always allowed |
| Constructors | ❌ No | ✔️ Yes |
| Best for | Capabilities (can-do) | Base behavior (is-a) |
🎯 Interview Pro Tip
✅ Say “interface = behavior/capability contract, abstract class = base class for inheritance.”
✅ Always mention “Java allows multiple interfaces but only one parent class.”
✅ Mention “Java 8 made interfaces more powerful, but they still can’t have fields like abstract classes.”