Abstract Class vs Interface in Java: Key Differences and Usage
abstract class can have both abstract and concrete methods and can hold state with fields, while an interface defines a contract with abstract methods and default/static methods but no instance state. Abstract classes support single inheritance, whereas interfaces support multiple inheritance of type.Quick Comparison
This table summarizes the main differences between abstract class and interface in Java.
| Factor | Abstract Class | Interface |
|---|---|---|
| Purpose | To share common code and define partial implementation | To define a contract for classes to implement |
| Methods | Can have abstract and concrete methods | All methods are abstract by default; can have default and static methods since Java 8 |
| Fields | Can have instance variables with any access modifier | Can only have public static final constants |
| Inheritance | Supports single inheritance (extends one class) | Supports multiple inheritance (implements multiple interfaces) |
| Constructors | Can have constructors | Cannot have constructors |
| Access Modifiers | Methods can have any access modifier | Methods are implicitly public |
Key Differences
An abstract class in Java is like a partially built blueprint. It can have both methods with code (concrete) and methods without code (abstract). It can also hold state through instance variables. This allows subclasses to inherit common behavior and override or add specific features.
On the other hand, an interface is a pure contract that classes agree to follow. Before Java 8, interfaces could only declare abstract methods without any implementation. Since Java 8, interfaces can have default methods with code and static methods, but they cannot hold instance state. Interfaces allow a class to promise to provide certain behaviors without forcing a class hierarchy.
Another key difference is inheritance: a class can extend only one abstract class but can implement multiple interfaces. This makes interfaces useful for adding multiple capabilities to a class. Also, abstract classes can have constructors and protected members, while interfaces cannot.
Code Comparison
abstract class Animal { String name; Animal(String name) { this.name = name; } abstract void sound(); void sleep() { System.out.println(name + " is sleeping"); } } class Dog extends Animal { Dog(String name) { super(name); } @Override void sound() { System.out.println(name + " says: Woof Woof"); } } public class Main { public static void main(String[] args) { Dog dog = new Dog("Buddy"); dog.sound(); dog.sleep(); } }
Interface Equivalent
interface Animal { void sound(); default void sleep() { System.out.println("Animal is sleeping"); } } class Dog implements Animal { private String name; Dog(String name) { this.name = name; } @Override public void sound() { System.out.println(name + " says: Woof Woof"); } @Override public void sleep() { System.out.println(name + " is sleeping"); } } public class Main { public static void main(String[] args) { Dog dog = new Dog("Buddy"); dog.sound(); dog.sleep(); } }
When to Use Which
Choose an abstract class when: you want to share common code among closely related classes, need to maintain state, or require controlled access with protected members. It is ideal when classes share a common base and behavior.
Choose an interface when: you want to define a contract for unrelated classes to implement, need multiple inheritance of type, or want to specify capabilities without enforcing a class hierarchy. Interfaces are best for defining roles or abilities that many classes can share.