Abstract Classes and Interfaces
Abstract classes and interfaces define what types must do. Choosing well keeps code flexible.
On this page · 5 min · Intermediate
On this page
- In simple words
- Learning objectives
- Explanation
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Intermediate
- Updated
- Oct 30, 2026
Related tutorials
In simple words#
An abstract class is a half-built house you finish (shared walls and wiring included). An interface is a certificate listing skills — any class with the skills can hold it, and a class can hold many certificates.
- abstract class
- Can’t be instantiated; may hold state and code.
- interface
- A contract; a class can implement many.
- default method
- Interface method with a body.
Learning objectives#
- Write abstract classes
- Write and implement interfaces
- Use default methods
- Choose between them
Explanation#
abstract class: may have fields, constructors, concrete and abstract methods. A class extends only one.
interface: methods are abstract by default; can have default and static methods; constants only. A class implements many.
Use an interface for a capability (Comparable, Payable, Notifier). Use an abstract class for a shared base with state.
Most Java frameworks (Spring) are built around interfaces.
Code example: Notifier interface + abstract base#
interface Notifier {
void send(String to, String message);
default void sendAll(java.util.List<String> recipients, String message) {
recipients.forEach(r -> send(r, message));
}
}
abstract class LoggingNotifier implements Notifier {
private int sent;
protected void log(String to) { sent++; System.out.println("[" + sent + "] sent to " + to); }
}
class SmsNotifier extends LoggingNotifier {
public void send(String to, String message) {
// call SMS provider here
log(to);
}
}
public class App {
public static void main(String[] args) {
Notifier n = new SmsNotifier();
n.sendAll(java.util.List.of("+91 98xxxxxx01", "+91 98xxxxxx02"), "Your order has shipped");
}
}
[1] sent to +91 98xxxxxx01 [2] sent to +91 98xxxxxx02
This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.
Step-by-step explanation#
- The interface defines send and a default sendAll.
- The abstract class adds shared logging state.
- SmsNotifier only implements send.
- Callers depend on the Notifier interface.
Real-world use case#
Spring services depend on interfaces such as PaymentGateway, so test versions can replace real ones.
Common mistakes#
A class can extend only one. Use interfaces for capabilities.
Split them into small, focused contracts.
Best practices#
- Program to interfaces
- Small, focused interfaces
- Abstract classes for shared state
- Default methods for convenience only
Remember this#
Interface = can-do contract (many).
Abstract class = shared base (one).
Depend on interfaces.
Practice exercise#
Your task: Create an EmailNotifier that implements Notifier and prints "Email to …".
Show solution
class EmailNotifier implements Notifier {
public void send(String to, String message) {
System.out.println("Email to " + to + ": " + message);
}
}
Mini quiz#
Which can a class implement many of?
Want more? Practise all Java MCQs · Java interview questions
My notes#
Saved in this browser.