惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Recent Announcements
Recent Announcements
J
Java Code Geeks
雷峰网
雷峰网
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
腾讯CDC
博客园 - 司徒正美
B
Blog RSS Feed
博客园 - 三生石上(FineUI控件)
I
InfoQ
N
Netflix TechBlog - Medium
L
LangChain Blog
博客园_首页
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Tailwind CSS Blog
MyScale Blog
MyScale Blog
美团技术团队
The Cloudflare Blog
爱范儿
爱范儿
Stack Overflow Blog
Stack Overflow Blog
博客园 - 聂微东
H
Help Net Security
Martin Fowler
Martin Fowler
V
Visual Studio Blog

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Java Interface
DHANRAJ S · 2026-06-16 · via DEV Community

DHANRAJ S

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

  1. One customer pays ₹2500 using card
  2. Another pays ₹1200 using UPI
  3. 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.