Interface is like a contract for what a class must be able to do. It declares method signatures, and when a class implements it, that class has to write those methods. It's like the interface saying, “If you're going to deliver, you must have a deliver() method.”
java
interface Notifier {
void send(String message);
}
class EmailNotifier implements Notifier {
public void send(String message) {
System.out.println("Email sent: " + message);
}
}
class SmsNotifier implements Notifier {
public void send(String message) {
System.out.println("SMS sent: " + message);
}
}
public class Main {
public static void main(String[] args) {
Notifier notifier = new EmailNotifier();
notifier.send("Welcome to Java!");
}
}Notifier interface requires a send method. EmailNotifier and SmsNotifier implement the interface, each writing its own version of the send behavior.
You should see
Email sent: Welcome to Java!Real-World Use
Using interfaces for interchangeable systems like Email/SMS/Push notifications, Stripe/PayPal payments, or local/cloud storage providers keeps your architecture clean.