Abstract class or interface? How to choose
30 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)An abstract class is a partly built parent for one family: it can hold fields, constructors and shared code. An interface is a contract for an ability that any class can add. Choosing comes down to one question: is this a family, or an ability?
What they have in common
Both are tools for abstraction, so they share a lot: neither can be instantiated, both can force methods on their classes, and both work as types.
Employee e = new Employee();// error: Employee is abstract; cannot be instantiatedChargeable c = new Chargeable();// error: Chargeable is abstract; cannot be instantiated
Employee[] staff = new Employee[2]; // fine: both work as typesChargeable phone = new Phone(); // fine: a class that implements itBoth can also have methods with a body: concrete methods in an abstract class, and default, static and private methods in an interface, as in default, static and private methods (opens in a new tab).
Where they differ
The differences are about state, how many and access. A phone that is both a camera and a music player shows the "how many" rule at once.
Example 1 · Mistake: trying to extend two abstract classes
abstract class Camera { abstract void click();}
abstract class MusicPlayer { abstract void play(String song);}
class SmartPhone extends Camera, MusicPlayer { void click() { System.out.println("Click"); } void play(String song) { System.out.println("Playing " + song); }}
public class TwoParentsDemo { public static void main(String[] args) { new SmartPhone().click(); }}TwoParentsDemo.java:9: error: '{' expected
class SmartPhone extends Camera, MusicPlayer {
^
1 errorWhat this code does
extendstakes exactly one class, so javac stops at the comma.- Fix: make
CameraandMusicPlayerinterfaces (they have no state) and writeimplements Camera, MusicPlayerwithpublicmethods.
All the differences, each one checked by javac:
- State: an abstract class can have normal fields and constructors. An interface has only
public static finalconstants and no constructor. - How many: a class extends only one abstract class, but can implement many interfaces.
- Access: abstract class methods can be
protectedor package-level. Interface methods arepublic(orprivatehelpers);protectedgivesmodifier protected not allowed here. - Meaning: an abstract class says what something is (an IS-A family). An interface says what something can do (an ability).
Family or ability?
Ask one question first: do the classes form a family that shares fields and code, or do unrelated classes share one ability? A family gets an abstract class; an ability gets an interface.
Example 2 · Abstract class: a family with shared state
abstract class Employee { protected String name; protected int salary;
Employee(String name, int salary) { this.name = name; this.salary = salary; }
final void printSlip() { System.out.println(name + ": Rs. " + (salary + bonus())); }
abstract int bonus();}
class FullTime extends Employee { FullTime(String name, int salary) { super(name, salary); } @Override int bonus() { return salary / 10; }}
class Intern extends Employee { Intern(String name, int salary) { super(name, salary); } @Override int bonus() { return 1000; }}
public class EmployeeDemo { public static void main(String[] args) { Employee[] staff = { new FullTime("Kavya", 60000), new Intern("Rohan", 15000) }; for (Employee e : staff) { e.printSlip(); } }}Kavya: Rs. 66000 Rohan: Rs. 16000
What this code does
- Every employee has a name, a salary and the same slip format: shared state and code, so an abstract class fits.
- An interface could not hold
nameandsalaryor a constructor that sets them.
Example 3 · Interface: an ability across unrelated classes
interface Chargeable { int batteryPercent(); void charge();}
class Phone implements Chargeable { int battery = 20; public int batteryPercent() { return battery; } public void charge() { battery = 100; }}
class Vehicle { String number = "KA05EV1234";}
class ElectricScooter extends Vehicle implements Chargeable { int battery = 45; public int batteryPercent() { return battery; } public void charge() { battery = 90; }}
public class ChargingStationDemo { static void chargeIfLow(Chargeable c) { if (c.batteryPercent() < 50) { c.charge(); } System.out.println("Battery now " + c.batteryPercent() + "%"); }
public static void main(String[] args) { chargeIfLow(new Phone()); chargeIfLow(new ElectricScooter()); }}Battery now 100% Battery now 90%
What this code does
- A phone and a scooter are not one family; the scooter already extends
Vehicle. Charging is just an ability they share. - An abstract class
Chargeablewould not work here:ElectricScootercannot extend a second class.
Rules of thumb, in order; the first "yes" usually decides:
- Do the classes share fields or a constructor? → abstract class (only it can hold state).
- Is it an ability that unrelated classes share, like
PayableorChargeable? → interface. - Does one class need several such types? → interfaces, since only one class can be extended.
- Not sure? → start with an interface. You can add an abstract base class later for shared code.
Using both together
An abstract base class is an abstract class that implements an interface and holds the fields and code shared by some of its classes. Concrete classes extend it and fill in the rest; other classes may still implement the interface directly.
A checkout uses the Payment interface. UPI and card payments share an account field and a fee rule, so they extend an abstract OnlinePayment implements Payment. Cash shares nothing with them, so it implements Payment directly.
Example 4 · Both together: interface + abstract base class
interface Payment { void pay(int amount);}
abstract class OnlinePayment implements Payment { protected String account;
OnlinePayment(String account) { this.account = account; }
public final void pay(int amount) { System.out.println(gateway() + " | " + account + " | Rs. " + (amount + fee(amount))); }
abstract String gateway(); abstract int fee(int amount);}
class UpiPayment extends OnlinePayment { UpiPayment(String upiId) { super(upiId); } @Override String gateway() { return "UPI"; } @Override int fee(int amount) { return 0; }}
class CardPayment extends OnlinePayment { CardPayment(String card) { super(card); } @Override String gateway() { return "Card"; } @Override int fee(int amount) { return amount * 2 / 100; }}
class CashPayment implements Payment { public void pay(int amount) { System.out.println("Cash | counter | Rs. " + amount); }}
public class BothDemo { public static void main(String[] args) { Payment[] payments = { new UpiPayment("diya@upi"), new CardPayment("XXXX-4321"), new CashPayment() }; for (Payment p : payments) { p.pay(1000); } }}UPI | diya@upi | Rs. 1000 Card | XXXX-4321 | Rs. 1020 Cash | counter | Rs. 1000
What this code does
Paymentis the contractmainuses.OnlinePaymentimplements it once with shared state (account) and a fixed format.CashPaymenthas nothing in common with online payments, so it implementsPaymentdirectly. All three fit in onePayment[].
An abstract class that implements an interface may leave some of its methods unimplemented; its concrete children must finish them. The checkout code sees only Payment, so it works with every payment type and never changes when a new one arrives.
Next step
Check your understanding
12 quick questions