today we discuss about Interface in Java.
first we understand the concept with simple Analogy,
Imagine you go to a shop and buy items.
in a bill counter, the shop keeper care about only one thing.
The customer paid the Money or not.
The shopkeeper does NOT care about how you pay the money,
UPI
Debit Card
Cash
They only thing is payment paid in successfully.
Here a interface acts like a Rule in billing counter.
It only defines what must be done, not how it should be done.
Different payment methods follow the same rule, but each one works in its own way.
The shopkeeper does not need to change anything in the billing counter.
No matter how the customer pays, the system works the same.
so, i follow this analogy and using a example for this blog.
What is Interface? (in GeeksforGeeks)
An interface in Java is a blueprint that defines a set of methods a class must implement without providing full implementation details. It helps achieve abstraction by focusing on what a class should do rather than how it does it. Interfaces also support multiple inheritance in Java.
- A class must implement all abstract methods of an interface.
- All variables in an interface are public, static, and final by default.
- Interfaces can have default, static, and private methods
first create a interface file Payment.java
public interface Payment {
void pay(int amount);
}
here we create a method but not defined that method
This is the shop rule.
“Anyone wants to pay must follow one rule → pay the amount.”
The shop does not explain how you pay, only thing is you must pay.
next we create another file for Different Customers,
class CardPayment implements Payment {
public void pay(int amount) {
System.out.println("Paid ₹" + amount + " using Card");
}
}
class UpiPayment implements Payment {
public void pay(int amount) {
System.out.println("Paid ₹" + amount + " using UPI");
}
}
class CashPayment implements Payment {
public void pay(int amount) {
System.out.println("Paid ₹" + amount + " using Cash");
}
}
java
These are the different methods for customers can pay:
- One customer uses Card
- One uses UPI
- One uses Cash
Each customer follows the same rule, but pays in a different way.
The objects for represent real customers standing at the counter with their payment method.
- One customer using card
- One customer using UPI
- One customer using cash
so next we create a class for PaymentApp,
how the customer pay the amount in the counter.
public class PaymentApp {
public static void processPayment(Payment payment, int amount) {
payment.pay(amount);
}
public static void main(String[] args) {
CardPayment card = new CardPayment();
UpiPayment upi = new UpiPayment();
CashPayment cash = new CashPayment();
processPayment(card, 2500);
processPayment(upi, 1200);
processPayment(cash, 500);
}
}
plaintext
The processPayment method is the billing counter.
Here the counter says:
“I don’t care how you pay.
Just give me something that follows the payment rule.”
Because of this:
Card works
UPI works
Cash works
Next the Main is
- One customer pays ₹2500 using card
- Another pays ₹1200 using UPI
- Another pays ₹500 using cash
The same counter handles all payments without any change.
Shop does not change for new payment methods,
Tomorrow, if a new customer pays using Wallet, the shop still works.
here i will show you the o/p,
user@boss:~/Desktop/java$ javac Payment.java
user@boss:~/Desktop/java$ javac PaymentService.java
user@boss:~/Desktop/java$ javac PaymentApp.java
user@boss:~/Desktop/java$ java PaymentApp
Paid ₹2500 using Card
Paid ₹1200 using UPI
Paid ₹500 using Cash
What we understand today
The interface is like a shop rule, and different payment methods follow that rule in their own way.
Top comments (0)