Java Programming · Lesson 9 of 15 · Updated ·
Abstract Classes and Interfaces in Java — Differences and When to Use Each
Abstraction means showing what something does and hiding how. You use a phone's "call" button without knowing the radio protocols. Java gives two tools for abstraction: abstract classes and interfaces.
1. Abstract classes
An abstract class can't be instantiated and may contain abstract methods (no body) that subclasses must implement. It can also have normal methods, fields and constructors — shared code for all children.
abstract class Payment {
double amount;
Payment(double amount) { this.amount = amount; }
abstract String pay(); // each payment type decides how
String receipt() { // shared code
return "Paid ₹" + amount + " via " + pay();
}
}
class UpiPayment extends Payment {
String upiId;
UpiPayment(double amount, String upiId) { super(amount); this.upiId = upiId; }
String pay() { return "UPI (" + upiId + ")"; }
}
class CardPayment extends Payment {
CardPayment(double amount) { super(amount); }
String pay() { return "card"; }
}
public class Main {
public static void main(String[] args) {
Payment[] payments = { new UpiPayment(499, "asha@upi"), new CardPayment(1299) };
for (Payment p : payments) System.out.println(p.receipt());
// new Payment(10); ← compile error: Payment is abstract
}
}
Paid ₹499.0 via UPI (asha@upi)
Paid ₹1299.0 via card
2. Interfaces
An interface is a contract: "any class that implements me promises to have these methods". A class can implement many interfaces.
interface Shareable {
String shareLink(); // abstract by default
default String shareText() { // default method (Java 8+)
return "Check this out: " + shareLink();
}
}
interface Downloadable {
String download();
}
class Certificate implements Shareable, Downloadable {
String id;
Certificate(String id) { this.id = id; }
public String shareLink() { return "skills101.in/certificates/" + id; }
public String download() { return id + ".pdf"; }
}
public class Main {
public static void main(String[] args) {
Certificate c = new Certificate("SK101-42");
System.out.println(c.shareText());
System.out.println(c.download());
Shareable s = c; // use the object through its interface
System.out.println(s.shareLink());
}
}
Check this out: skills101.in/certificates/SK101-42
SK101-42.pdf
skills101.in/certificates/SK101-42
- Interface methods are
public abstractby default; fields arepublic static final(constants). - Implementing methods must be
public.
3. Which one should I use?
- Use an abstract class when classes share state and code and have an "is-a" relationship (
UpiPaymentis aPayment). - Use an interface for a capability many unrelated classes can have ("can be shared", "can be compared", "can be run").
- Programming to interfaces (
List<String> list = new ArrayList<>()) keeps your code flexible.
4. Functional interfaces (preview of lambdas)
An interface with exactly one abstract method is a functional interface. You can implement it with a lambda:
interface Discount {
double apply(double price);
}
public class Main {
public static void main(String[] args) {
Discount festive = p -> p * 0.8;
Discount flat = p -> p - 100;
System.out.println(festive.apply(1000) + " " + flat.apply(1000));
}
}
800.0 900.0
More in the Java 8 lesson.
Common mistakes
- Trying to create an object of an abstract class or interface with
new. - Forgetting to implement every abstract method (the class must then be abstract itself).
- Making implemented interface methods non-public.
Quick recap
- Abstract class: can't instantiate; abstract + normal methods; fields; constructors; extend one.
- Interface: contract; abstract, default, static methods; constants; implement many.
- Abstract class for shared code ("is-a"); interface for abilities ("can-do").
- One abstract method → functional interface → usable with lambdas.