今日已更新 233 条资讯 | 累计 20205 条内容
关于我们

Java Interface

DHANRAJ S 2026年06月16日 02:47 4 次阅读 来源:Dev.to

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 ₹" +

本文内容来源于互联网,版权归原作者所有
查看原文