Difference Between Abstract Class and Interface in Java
Both abstract classes and interfaces let you define methods that subclasses must implement, and neither can be instantiated directly. They serve different purposes, though:
- An abstract class is a partially implemented base class for closely related classes. It can hold state (fields), constructors and code shared by its subclasses. A class can extend only one.
- An interface is a contract or capability that any class can adopt, whatever its hierarchy. It has no instance state, and a class can implement many.
Since Java 8 interfaces can contain default and static methods, which narrowed the gap, but the key differences remain. This page compares them in detail with examples.
1. Comparison Table
| Aspect | Abstract class | Interface |
|---|---|---|
| Keyword | abstract class; used with extends | interface; used with implements |
| Multiple inheritance | A class extends only one class | A class implements any number; an interface can extend many |
| Methods | Abstract and concrete methods | Abstract, default (8), static (8) and private (9) methods |
| Fields | Any: instance, static, final or mutable | Only public static final constants |
| Constructors | Yes | No |
| Access modifiers | Any (private, protected…) | Members are public (private helper methods allowed) |
| State | Can hold per-object state | Stateless |
| Relationship | "is-a" (strong, same family) | "can-do" / capability (any class) |
| Lambdas | Cannot be a lambda target | Functional interfaces can |
| Evolution | Adding a concrete method is safe | Adding an abstract method breaks implementers (use default) |
2. Both in One Example
Employee is an abstract class with a name, a constructor and shared code; Payable and Auditable are interfaces that unrelated types can implement:
public class AbstractVsInterface {
// Interface: a capability any class can have
interface Payable {
double amount();
default String receipt() { return String.format("Paid %.2f", amount()); } // Java 8 default
}
interface Auditable { String auditTag(); }
// Abstract class: shared state + constructor + partial implementation
static abstract class Employee {
protected final String name;
private static int count;
Employee(String name) { this.name = name; count++; } // constructor
abstract double monthlyPay(); // must be implemented
String describe() { return name + " earns " + monthlyPay(); }
static int count() { return count; }
}
// One superclass, many interfaces
static class Manager extends Employee implements Payable, Auditable {
Manager(String name) { super(name); }
double monthlyPay() { return 150_000; }
public double amount() { return monthlyPay(); }
public String auditTag() { return "MGR-" + name.toUpperCase(); }
}
// An unrelated class can be Payable too
record Invoice(String id, double amount) implements Payable { }
public static void main(String[] args) {
Manager m = new Manager("Asha");
System.out.println(m.describe());
System.out.println(m.receipt() + " | " + m.auditTag());
Payable[] bills = { m, new Invoice("INV-7", 4999.5) }; // same contract, different hierarchies
for (Payable p : bills) System.out.println(p.getClass().getSimpleName() + ": " + p.receipt());
System.out.println("Employees created: " + Employee.count());
}
}
Asha earns 150000.0 Paid 150000.00 | MGR-ASHA Manager: Paid 150000.00 Invoice: Paid 4999.50 Employees created: 1
Managerextends one class but implements two interfaces.Invoiceis a record, not an employee, yet it isPayabletoo: interfaces cut across hierarchies.- The default method
receipt()is shared by everyPayablewithout a common superclass.
3. Using Them Together: Skeletal Implementation
The choice is often not either/or. A common design defines the type as an interface and provides an abstract class that implements the boring parts, exactly as the JDK does with List and AbstractList:
public class SkeletalImplementation {
interface Shape { // the contract (type)
double area();
String name();
}
static abstract class AbstractShape implements Shape { // reusable partial implementation
private final String name;
AbstractShape(String name) { this.name = name; }
public String name() { return name; }
@Override public String toString() { return String.format("%s (area %.2f)", name, area()); }
}
static class Circle extends AbstractShape {
private final double r;
Circle(double r) { super("Circle"); this.r = r; }
public double area() { return Math.PI * r * r; }
}
static class Square extends AbstractShape {
private final double s;
Square(double s) { super("Square"); this.s = s; }
public double area() { return s * s; }
}
public static void main(String[] args) {
Shape[] shapes = { new Circle(1.5), new Square(3) };
for (Shape s : shapes) System.out.println(s);
}
}
Circle (area 7.07) Square (area 9.00)
Clients depend on Shape; implementers can extend AbstractShape for convenience or implement Shape directly.
4. When to Use Which
| Choose an interface when… | Choose an abstract class when… |
|---|---|
You define a capability for unrelated classes (Comparable, Runnable, AutoCloseable) | Closely related classes share code and fields |
| A class needs several types at once | You need constructors or non-public members |
| You want lambdas (functional interface) | You want a template method that fixes an algorithm's structure |
| You publish an API others will implement | You control all subclasses and want to share state |
A good default: start with an interface, and add an abstract class only when implementations need to share state or substantial code.
5. What Changed in Java 8 and Later?
- Java 8: default and static methods in interfaces, so interfaces can carry behaviour and evolve safely.
- Java 9: private interface methods for sharing code between defaults.
- Java 17: both abstract classes and interfaces can be
sealedto restrict their implementations.
Interfaces still cannot have instance fields or constructors, which is the main reason abstract classes remain useful.
6. Summary
- Abstract classes share state and code among related classes; a class can extend only one.
- Interfaces define contracts any class can implement; a class can implement many.
- Abstract classes have constructors and any fields; interfaces have only constants.
- Combine them: interface for the type, abstract class for a skeletal implementation.
Related Tutorials
Frequently Asked Questions (FAQ)
Q1. What is the main difference between an abstract class and an interface?
An abstract class can have state, constructors and any kind of method, and a class can extend only one. An interface defines a stateless contract, and a class can implement many.
Q2. Can an interface have a constructor?
No. Interfaces have no instance state to initialise. Abstract classes can have constructors.
Q3. Can an abstract class implement an interface?
Yes, and it does not have to implement all the interface's methods; concrete subclasses complete them.
Q4. Can an interface extend an abstract class?
No. Interfaces can only extend other interfaces.
Q5. Can an interface have instance variables?
No. Any field declared in an interface is implicitly public static final.
Q6. Why does Java allow multiple interfaces but not multiple classes?
Inheriting state and constructors from several classes causes ambiguity (the diamond problem). Interfaces carry no state, and default method conflicts must be resolved explicitly.
Q7. After Java 8, is an interface the same as an abstract class?
No. Interfaces gained method bodies, but they still cannot have instance fields, constructors or protected members, and classes can still extend only one class.
Q8. Which is faster, an abstract class or an interface?
There is no meaningful difference in modern JVMs. Choose based on design.
Q9. Can both have static methods?
Yes. Static interface methods (Java 8) are called on the interface and are not inherited by implementing classes.
Q10. Can both have private methods?
Yes. Abstract classes always could; interfaces can since Java 9, as helpers for default and static methods.
Q11. Can an abstract class have zero abstract methods?
Yes. It is then abstract only to prevent instantiation.
Q12. Can an interface be declared final?
No. Interfaces exist to be implemented. Use sealed to restrict implementations instead.
Q13. Which supports lambdas?
Only functional interfaces (one abstract method). Abstract classes cannot be implemented with lambdas.
Q14. What are examples of each in the JDK?
Interfaces: List, Runnable, Comparable. Abstract classes: AbstractList, InputStream, Number.
Q15. What happens if a class does not implement all methods?
It must be declared abstract, whether the methods come from an abstract superclass or an interface.
Q16. Can an interface have protected methods?
No. Interface members are public, except private helper methods.
Q17. Is it better to program to an interface or an abstract class?
Usually to an interface, because it keeps code loosely coupled and lets any class provide an implementation.
Q18. What is a skeletal implementation?
An abstract class that implements an interface's common parts so concrete classes only fill in the essentials, like AbstractList for List.
Q19. How do I resolve two default methods with the same signature?
Override the method in the class and call the desired version with InterfaceName.super.method().
Q20. Is this a common interview question?
Yes, one of the most common. Mention state, constructors, multiple inheritance, Java 8 default methods and when to use each.