Difference Between an Abstract Class and an Interface in Java
Feature | Abstract Class | Interface |
---|---|---|
Inheritance Type | Supports single inheritance | Supports multiple inheritance |
Methods | Can have both abstract and concrete methods | Can have abstract, default, and static methods (default and static since Java 8) |
Access Modifiers | Methods can have any access modifier (private , protected , public ) | Methods are implicitly public abstract unless default or static |
Fields | Can have instance variables and static fields | Only public static final (constants) |
Constructors | Can have constructors | Cannot have constructors |
State Maintenance | Can maintain state (instance variables) | Cannot maintain state (only constants) |
Use Case | When you want to provide a base class with shared behavior | When you want to define a contract that multiple classes can implement |
When to Use an Abstract Class vs. an Interface?
Use an Abstract Class When:
- You need to share code among related classes.
- You need constructors or instance variables.
- You want to provide a base implementation that subclasses can override or extend.
- You expect future changes in method implementations without affecting all subclasses.
✅ Example:
abstract class Animal {
String name;
Animal(String name) { // Constructor allowed
this.name = name;
}
void sleep() { // Concrete method
System.out.println(name + " is sleeping.");
}
abstract void makeSound(); // Abstract method (must be implemented by subclasses)
}
Use an Interface When:
- You need to define a contract that unrelated classes can implement.
- You require multiple inheritance (since Java allows multiple interfaces but not multiple abstract classes).
- You want to achieve pure abstraction (before Java 8).
- You are working with API design, where different classes should follow the same behavior.
✅ Example:
interface Flyable {
void fly(); // Implicitly public and abstract
}
class Bird implements Flyable {
public void fly() {
System.out.println("Bird is flying.");
}
}
Final Thoughts
- If you need default behavior, shared fields, and constructors → Use an abstract class.
- If you need multiple inheritance, a strict contract, and full abstraction → Use an interface.
- Java 8+ blurs the distinction since interfaces now support default and static methods.