Interface in Java – Complete Tutorial with Real-Life Examples (Beginner Friendly)
Last Updated on: 21st Nov 2025 19:22:34 PM
Welcome to the most easy-to-understand, complete tutorial on Interfaces in Java!
If you're learning OOP (Object-Oriented Programming), this guide will make you fall in love with interfaces — with super simple explanations and real-life examples you’ll never forget!
Let’s go!
What is an Interface in Java?
An interface is a 100% abstract blueprint that defines what a class must do, but not how to do it. It’s like a contract or set of rules that a class agrees to follow.
It contains abstract methods (method names only, no body). Classes that use the interface must implement those methods.
👉 Interface tells WHAT to do,
👉 Implementing class decides HOW to do it.
Why Use Interfaces?
Interfaces help in:
✔ Achieving 100% abstraction
✔ Multiple inheritance in Java
✔ Defining common behavior for multiple classes
✔ Reducing code dependency
✔ Building loosely-coupled applications
Key Features of Interface
✔ All methods are abstract by default (before Java 8)
✔ All variables are:
-
public -
static -
final
✔ A class can implement multiple interfaces
✔ Interfaces support default and static methods (Java 8+)
✔ Interfaces cannot have:
-
Constructors
-
Instance variables
-
Method bodies (except default/static)
Syntax of an Interface
interface MyInterface {
void method1(); // automatically public & abstract
int add(int a, int b); // no body → must be implemented
}
Real-Life Example You’ll Never Forget: Remote Control
Think of a TV Remote:
Every remote has these buttons:
-
Power On/Off
-
Volume Up/Down
-
Channel Change
But the same remote can control:
-
Samsung TV
-
LG TV
-
Sony TV
All TVs follow the same button rules, but how they work inside is different!
This is exactly what an interface does!
interface RemoteControl {
void powerOn();
void powerOff();
void volumeUp();
void volumeDown();
void changeChannel(int channel);
}
Now any TV brand can follow this remote:
class SamsungTV implements RemoteControl {
public void powerOn() { System.out.println("Samsung TV: Power ON"); }
public void powerOff() { System.out.println("Samsung TV: Power OFF"); }
public void volumeUp() { System.out.println("Samsung TV: Volume Increased"); }
public void volumeDown() { System.out.println("Samsung TV: Volume Decreased"); }
public void changeChannel(int channel) {
System.out.println("Samsung TV: Channel changed to " + channel);
}
}
class LGTV implements RemoteControl {
public void powerOn() { System.out.println("LG TV: Booting up..."); }
public void powerOff() { System.out.println("LG TV: Shutting down..."); }
public void volumeUp() { System.out.println("LG TV: Volume++"); }
public void volumeDown() { System.out.println("LG TV: Volume--"); }
public void changeChannel(int channel) {
System.out.println("LG TV: Now showing channel " + channel);
}
}
Now you can write universal code:
public class Home {
public static void main(String[] args) {
RemoteControl tv1 = new SamsungTV();
RemoteControl tv2 = new LGTV();
tv1.powerOn(); // Works for any TV!
tv2.changeChannel(7); // Works for any TV!
}
}
This is the power of interfaces!
Full Working Example: Payment System (Real-World App)
Imagine you're building an online shopping app like Amazon.
Users can pay using:
-
Credit Card
-
Debit Card
-
UPI
-
PayPal
All payment methods must have:
-
pay()
-
getPaymentStatus()
-
refund()
Let’s create an interface!
interface PaymentMethod {
void pay(double amount);
String getPaymentStatus();
boolean refund(double amount);
// Java 8+ feature: default method
default void printReceipt() {
System.out.println("Receipt printed successfully!");
}
// Java 8+ feature: static method
static void showSupportedMethods() {
System.out.println("Supported: Credit Card, UPI, PayPal");
}
}
Now implement different payment methods:
class CreditCard implements PaymentMethod {
String cardNumber;
public CreditCard(String cardNumber) {
this.cardNumber = cardNumber;
}
@Override
public void pay(double amount) {
System.out.println("Paid ₹" + amount + " using Credit Card ending " + cardNumber.substring(12));
}
@Override
public String getPaymentStatus() {
return "Payment Successful (Credit Card)";
}
@Override
public boolean refund(double amount) {
System.out.println("Refund of ₹" + amount + " processed to Credit Card");
return true;
}
}
class UPI implements PaymentMethod {
String upiId;
public UPI(String upiId) {
this.upiId = upiId;
}
@Override
public void pay(double amount) {
System.out.println("Paid ₹" + amount + " using UPI: " + upiId);
}
@Override
public String getPaymentStatus() {
return "Payment Successful (UPI)";
}
@Override
public boolean refund(double amount) {
System.out.println("Refund of ₹" + amount + " sent to UPI");
return true;
}
}
Now your app code:
public class ShoppingApp {
public static void main(String[] args) {
PaymentMethod card = new CreditCard("1234-5678-9012-3456");
PaymentMethod upi = new UPI("user@oksbi");
card.pay(2999.99);
System.out.println(card.getPaymentStatus());
card.printReceipt(); // from default method
System.out.println("---");
upi.pay(1500);
upi.refund(1500);
PaymentMethod.showSupportedMethods(); // static method
}
}
Output:
Paid ₹2999.99 using Credit Card ending 3456
Payment Successful (Credit Card)
Receipt printed successfully!
---
Paid ₹1500.0 using UPI: user@oksbi
Refund of ₹1500.0 sent to UPI
Supported: Credit Card, UPI, PayPal
Beautiful! Your app works with **any** payment method!
Easy Rule:
-
Use Abstract Class → When classes are closely related and share code
Example: Dog, Cat → both are Animal -
Use Interface → When unrelated classes need same behavior
Example: Car, Airplane, Bird → all can Flyable
Real-Life Example 2: Online Ordering System
All online ordering platforms follow actions like:
-
Add to cart
-
Place order
-
Make payment
-
Track order
But implementation differs between Amazon, Flipkart, Meesho, etc.
Interface Example
// Interface
interface OrderService {
void placeOrder();
void makePayment();
}
// Class 1: Amazon
class Amazon implements OrderService {
@Override
public void placeOrder() {
System.out.println("Order placed on Amazon.");
}
@Override
public void makePayment() {
System.out.println("Payment made via Amazon Pay.");
}
}
// Class 2: Flipkart
class Flipkart implements OrderService {
@Override
public void placeOrder() {
System.out.println("Order placed on Flipkart.");
}
@Override
public void makePayment() {
System.out.println("Payment made via PhonePe.");
}
}
// Main class to test
public class Main {
public static void main(String[] args) {
// Creating object of Amazon
OrderService amazonOrder = new Amazon();
amazonOrder.placeOrder();
amazonOrder.makePayment();
System.out.println("-------------------------");
// Creating object of Flipkart
OrderService flipkartOrder = new Flipkart();
flipkartOrder.placeOrder();
flipkartOrder.makePayment();
}
}
Output
Order placed on Amazon.
Payment made via Amazon Pay.
-------------------------
Order placed on Flipkart.
Payment made via PhonePe.
Multiple Inheritance with Interfaces (Java’s Superpower!)
A class can implement many interfaces!
interface Flyable {
void fly();
}
interface Swimmable {
void swim();
}
interface Quackable {
void quack();
}
class Duck implements Flyable, Swimmable, Quackable {
public void fly() { System.out.println("Duck flying high!"); }
public void swim() { System.out.println("Duck swimming"); }
public void quack() { System.out.println("Quack Quack!"); }
}
Duck can fly, swim, and quack → Real multiple inheritance!
Java does not support multiple inheritance with classes, but supports multiple inheritance using interfaces.
This means:
➡️ A class can implement multiple interfaces
➡️ That class will inherit abstract methods from all interfaces
➡️ This avoids the “Diamond Problem” and keeps things safe and clean
Real-Life Example: A Smartphone
A Smartphone is the best real-world example of multiple inheritance.
A Smartphone:
-
Works as a Camera
-
Works as a Music Player
-
Works as a GPS Navigator
-
Works as a Computer (Internet)
One device → behaves like many things.
Java allows this using interfaces.
Step 1: Create Interfaces
1. Camera Interface
interface Camera {
void takePhoto();
void recordVideo();
}
2. MusicPlayer Interface
interface MusicPlayer {
void playMusic();
void stopMusic();
}
3. GPS Interface
interface GPS {
void navigate(String location);
}
Step 2: Create a Class That Implements All Interfaces
Smartphone Class Using Multiple Inheritance
class Smartphone implements Camera, MusicPlayer, GPS {
@Override
public void takePhoto() {
System.out.println("Taking photo with 108MP camera...");
}
@Override
public void recordVideo() {
System.out.println("Recording 4K video...");
}
@Override
public void playMusic() {
System.out.println("Playing music...");
}
@Override
public void stopMusic() {
System.out.println("Stopping music...");
}
@Override
public void navigate(String location) {
System.out.println("Navigating to: " + location);
}
}
Step 3: Use the Smartphone Class
public class Main {
public static void main(String[] args) {
Smartphone phone = new Smartphone();
phone.takePhoto();
phone.recordVideo();
phone.playMusic();
phone.stopMusic();
phone.navigate("Delhi");
}
}
Output
Taking photo with 108MP camera...
Recording 4K video...
Playing music...
Stopping music...
Navigating to: Delhi
Why This Is the BEST Real-Life Example?
✔ One Smartphone behaves like different devices
✔ Each interface defines one specific behavior
✔ The Smartphone class collects all behaviors into one place
✔ Perfect demonstration of multiple inheritance using interfaces
Rules of Interfaces in Java
| Rule | Description |
|---|---|
| Cannot create object | ❌ |
| All variables are public static final | ✔ |
| All methods are abstract (unless default/static) | ✔ |
| Supports multiple inheritance | ✔ |
| Cannot have constructors | ❌ |
| Can extend another interface | ✔ |
| Class must implement all abstract methods | ✔ |
Concept Summary
| Feature | Interface | Class |
|---|---|---|
| Multiple Inheritance | ✔️ Allowed | ❌ Not Allowed |
| Methods | Abstract (Java 7), Default + Static allowed (Java 8+) | Concrete + abstract both |
| Variables | public static final only | Any type |
| Constructor | ❌ Not allowed | ✔️ Allowed |
Conclusion
An interface in Java:
-
Provides 100% abstraction
-
Defines common behavior
-
Allows multiple inheritance
-
Helps build flexible and loosely coupled applications
-
Used everywhere: JDBC, Collections, Threads, REST APIs
Keep practicing — you're doing amazing!
Happy Coding! ![]()