“What is the difference between an interface and an abstract class?” is one of the most common Java fresher interview questions. The textbook answer lists rules; a better answer also explains when you would choose each.
Abstract class
An abstract class is a class that cannot be instantiated directly. It can have fields, constructors, normal methods and abstract methods (methods without a body that subclasses must implement).
public abstract class Payment {
protected double amount;
public Payment(double amount) { this.amount = amount; }
public abstract boolean process();
public String receipt() { return "Paid: " + amount; }
}
Interface
An interface defines a contract: methods a class promises to provide. Since Java 8 it can also contain default and static methods, but it cannot hold instance state.
public interface Notifier {
void send(String message);
}
The key differences
- Multiple inheritance: a class can extend only one class but implement many interfaces.
- State: abstract classes can have instance fields; interfaces cannot (only constants).
- Constructors: abstract classes can have them; interfaces cannot.
- Relationship: extending an abstract class says “is a kind of”; implementing an interface says “can do”.
A simple rule for choosing
Use an abstract class when related classes share real code and state, such as card, UPI and net-banking payments that all store an amount and print a receipt. Use an interface when unrelated classes need the same capability, such as email, SMS and WhatsApp notifiers that all send() a message.
In Spring Boot you will mostly see interfaces: repositories, service contracts and many framework hooks. Programming to interfaces makes code easier to test, because you can swap in a fake implementation.
Interview tip
After stating the differences, give one example of each from a project you built. That shows you have used the idea, not just memorised it.
Related: the four pillars of OOP in Java and Java Full Stack interview questions.
These topics are practised with projects and mock interviews in our Java Full Stack course in Bangalore.
Book a free demo class at our BTM Layout centre, and ask a counsellor for batch details.
