Java interface vs abstract class, with a notice sender example and a table.
In one line
Decide only what has to be done and leave how to do it to later classes. In Java you do this with interface and abstract class.
Why you need it
Writing email and SMS notice code separately means repeating the same steps in each channel: drop blank messages, trim spaces, record how many were sent. Forget a check in one channel and only that channel sends empty notices.
The Notifier interface only promises "can send", and BaseNotifier writes the shared steps once. Each channel class fills in just deliver, the part that actually sends.
Everyday picture
Every country fixes the shape of its wall sockets. A company making appliances only has to match that shape (the interface); what happens inside is up to each maker. An abstract class is a half-built kit: "the power unit is already fitted, just build the feature part and attach it."
Example code
Main.java · 55 lines
import java.util.ArrayList;import java.util.List;interfaceNotifier {booleansend(String to, String message);}abstractclassBaseNotifierimplementsNotifier {privateint sent;@Overridepublicfinalbooleansend(String to, String message) {if (message.isBlank()) {returnfalse; }deliver(to, message.strip()); sent++;returntrue; }intsentCount() {return sent; }protectedabstractvoiddeliver(String to, String text);}classEmailNotifierextendsBaseNotifier {@Overrideprotectedvoiddeliver(String to, String text) {System.out.println("[email] " + to + " <- " + text); }}classSmsNotifierextendsBaseNotifier {@Overrideprotectedvoiddeliver(String to, String text) {String cut = text.length() > 20 ? text.substring(0, 20) + "..." : text;System.out.println("[sms] " + to + " <- " + cut); }}publicclassMain {publicstaticvoidmain(String[] args) {EmailNotifier email = newEmailNotifier();List<Notifier> channels = newArrayList<>(); channels.add(email); channels.add(newSmsNotifier());for (Notifier n : channels) { n.send("mina", " Your order has shipped today "); n.send("mina", " "); }System.out.println("email sent: " + email.sentCount()); }}
Java code does not run on this page. Compile your changed code with javac on your own computer.
interface vs abstract class
interface
abstract class
Create with new
X
X
Per-object fields
X
O
Methods with a body
default and static only
O
Constructors
X
O
How many a class can take
many, with implements
one, with extends
Any field written in an interface becomes a public static final constant.
When to use it
You name a job before its implementations exist (storage, payment, notices, file reading) → interface
Children share the same order of steps and fields and differ in one or two steps → abstract class
You want to swap in a fake implementation for tests → interface
A class has one parent class but can implements many interfaces. If you are unsure, start with an interface.
isBlank and strip are string methods added in Java 11.
Common mistakes
Creating an abstract class with new
BaseNotifier has no body for deliver, so it cannot be used as it is. Create a child class that fills in every abstract method.
Wrong
publicclassMain {publicstaticvoidmain(String[] args) {Notifier n = newBaseNotifier(); }}
What javac sayserror: BaseNotifier is abstract; cannot be instantiated
Fixed
publicclassMain {publicstaticvoidmain(String[] args) {Notifier n = newEmailNotifier(); n.send("mina", "hello"); }}
When you run it[email] mina <- hello
Filling the abstract method under another name
You wrote push, but the parent asks for deliver. Match the name and parameters exactly and add @Override.
Wrong
classPushNotifierextendsBaseNotifier {protectedvoidpush(String to, String text) {System.out.println("[push] " + to + " <- " + text); }}
What javac sayserror: PushNotifier is not abstract and does not override abstract method deliver(String,String) in BaseNotifier
Fixed
classPushNotifierextendsBaseNotifier {@Overrideprotectedvoiddeliver(String to, String text) {System.out.println("[push] " + to + " <- " + text); }}
Keeping changing state in an interface
Interface fields are automatically static final constants, so they cannot change. Put per-object values in a private field of an abstract or concrete class.
Wrong
interfaceNotifier {int sent = 0;defaultvoidcountUp() { sent++; }}
What javac sayserror: cannot assign a value to final variable sent
Fixed
interfaceNotifier {booleansend(String to, String message);}abstractclassBaseNotifierimplementsNotifier {privateint sent;protectedvoidcountUp() { sent++; }}
Check yourself
Question 1
Which one can hold a per-object field and pass it to children?
Show the answer
Answer: 2. abstract class Fields written in an interface are all public static final constants. Per-object values belong in an abstract class.
Question 2
Using the example classes, what does this print?
publicclassMain {publicstaticvoidmain(String[] args) {Notifier n = newSmsNotifier();System.out.println(n.send("joon", " ")); }}
Show the answer
Answer: 2. falsesend is final, so the SMS channel also runs the blank check first. A single space is blank, so only false is printed.
Questions people ask
Should I start with an interface or an abstract class?
Start with an interface when you only name the job. Add an abstract class once several implementations share the same steps or fields.
Can an interface have methods with a body?
Since Java 8, default and static methods are allowed. Per-object fields are still not.
Can I create an object from an abstract class?
Not directly with new. Create a subclass that fills in every abstract method.