Difference Between Abstract Class and Interface
"What is the difference between an abstract class and an interface?" may be the most asked Java interview question of all. Since Java 8 gave interfaces default methods, the two look more alike than they used to, so the answer has changed. The short version: an interface defines what a type can do and can be added to any class; an abstract class is a partly built class that shares state and code with a family of subclasses.
Side by Side
| Abstract class | Interface | |
|---|---|---|
| Keyword | abstract class, used with extends | interface, used with implements |
| How many per class | One (single inheritance) | Many |
| Instance fields (state) | Yes | No, only public static final constants |
| Constructors | Yes | No |
| Methods with code | Yes, any | default, static and private methods only (Java 8 and 9) |
| Abstract methods | Optional; marked abstract | Methods without a body are implicitly public abstract |
| Access of members | Any: private, protected, package, public | public (or private helpers) |
final methods | Allowed | Not allowed |
| Relationship expressed | Is-a, a family: Contractor is an Employee | Can-do, an ability: Billable, Comparable |
| Adding a new method later | Easy with a body; subclasses inherit it | Use a default method, or every implementer breaks |
How to Choose
- Choose an interface when unrelated classes need the same ability, when a class may need several such abilities, or when you're defining a type others will implement.
- Choose an abstract class when closely related classes share fields, constructors or protected helper methods, or you want to lock steps with
finaltemplate methods. - When in doubt, start with an interface. A class can implement it without giving up its only
extends.
Using Both: The Skeletal Implementation
Library designers often offer both: an interface as the public type, and an abstract class that implements it partly, so writing a new implementation is quick. The JDK does this with List and AbstractList, and Map and AbstractMap.
public class Skeleton {
interface PaymentMethod {
String pay(long amount);
int count();
}
abstract static class BasePayment implements PaymentMethod {
private final String label;
private int count;
BasePayment(String label) { this.label = label; }
public final String pay(long amount) {
count++;
return label + " #" + count + ": " + process(amount);
}
public int count() { return count; }
protected abstract String process(long amount);
}
static class Upi extends BasePayment {
Upi() { super("UPI"); }
protected String process(long amount) {
return "Rs. " + amount + " sent";
}
}
static class Card extends BasePayment {
Card() { super("Card"); }
protected String process(long amount) {
return "Rs. " + (amount + amount * 2 / 100) + " charged";
}
}
public static void main(String[] args) {
PaymentMethod upi = new Upi();
PaymentMethod card = new Card();
System.out.println(upi.pay(500));
System.out.println(upi.pay(120));
System.out.println(card.pay(1000));
System.out.println("UPI payments: " + upi.count());
}
}UPI #1: Rs. 500 sent
UPI #2: Rs. 120 sent
Card #1: Rs. 1020 charged
UPI payments: 2How it works
PaymentMethodis the type everyone uses;mainnever mentionsBasePayment.BasePaymentholds the state an interface can't: a label and a counter, set up by a constructor.- Its
finalpaymethod fixes the steps; subclasses only supplyprocess, which isprotected, another thing an interface can't offer. - A class that already extends something else could still implement
PaymentMethoddirectly, without the skeleton.
Common Mistakes
Answering with the pre-Java 8 list. "Interfaces can't have method bodies" is no longer true; mention default, static and private methods.
Choosing an abstract class for an ability. abstract class Billable would stop a SchoolFee that already extends something else from being billable.
Putting state in an interface through constants. Interface fields are shared constants, not per-object data.
Interview Corner
What is the difference between an abstract class and an interface?
A class can extend only one abstract class but implement many interfaces. Abstract classes can have instance fields, constructors, methods of any access level, and final methods; interfaces have no instance state or constructors, their abstract methods are public, and since Java 8 and 9 they can have default, static and private methods. Abstract classes model an is-a family that shares state and code; interfaces model a capability that unrelated classes can share.
When would you use an abstract class instead of an interface?
When closely related classes share fields, constructor logic or non-public helper methods, or when you want a final template method that fixes an algorithm's steps. Often the best design uses both: an interface as the public type and an abstract class as a skeletal implementation, like List and AbstractList.
After Java 8, why not use interfaces for everything?
Interfaces still can't hold instance state, can't have constructors, and can't have protected or final methods. Default methods can only build on other interface methods, so behavior that needs fields still belongs in a class.
Practice
- Easy: For each, choose abstract class or interface and say why:
Shapewith a stored color;Printable;Vehiclewith a registration number;Comparable. - Harder: Add a
NetBankingclass toSkeleton.javawith a flat fee of Rs. 10, then add aWalletclass that implementsPaymentMethoddirectly withoutBasePayment.
Key Takeaways
Was this tutorial helpful?