DEV Community

bala d kaveri
bala d kaveri

Posted on

πŸš€ Day 34 of My Automation Journey – Interfaces (Real Programs)

Today’s focus was on one of the core OOP concepts – Interfaces πŸ”₯

Instead of only theory, I practiced real-world scenarios using interfaces and understood how they help in scalability, flexibility, and team development.


πŸ”Ή What is an Interface?

πŸ‘‰ An interface is a blueprint of a class

βœ” Contains abstract methods
βœ” Provides 100% abstraction (conceptually)
βœ” Used to achieve loose coupling


πŸ”Ή Why Interface?

πŸ‘‰ When multiple classes have:
βœ” Same behavior
βœ” Different implementations

πŸ’‘ Interface helps define a common contract


πŸ”Ή Q1: How does Interface help in Payment Systems?

🧠 Scenario:

Online shopping app supports:

  • UPI
  • Credit Card
  • PayPal

πŸ‘‰ All do payment, but differently


βœ… Program:

package interfacedemo;

public class PaymentApp {

    interface Payment {
        void pay(int amount);
    }

    static class UpiPayment implements Payment {
        public void pay(int amount) {
            System.out.println("UPI Payment: " + amount);
        }
    }

    static class CreditCardPayment implements Payment {
        public void pay(int amount) {
            System.out.println("Card Payment: " + amount);
        }
    }

    static class PaypalPayment implements Payment {
        public void pay(int amount) {
            System.out.println("PayPal Payment: " + amount);
        }
    }

    public static void main(String[] args) {

        Payment p = new UpiPayment();
        p.pay(500);

        Payment p1 = new CreditCardPayment();
        p1.pay(1000);

        Payment p2 = new PaypalPayment();
        p2.pay(1500);
    }
}
Enter fullscreen mode Exit fullscreen mode

βœ… Output:

UPI Payment: 500
Card Payment: 1000
PayPal Payment: 1500
Enter fullscreen mode Exit fullscreen mode

πŸ’‘ Explanation:

πŸ‘‰ Interface Payment defines common method pay()
πŸ‘‰ Different classes implement it differently
πŸ‘‰ Easy to add new payment types in future


πŸ”Ή Q2: How Interface Works in Vehicles?

🧠 Scenario:

  • Car starts
  • Bike starts πŸ‘‰ Same action, different behavior

βœ… Program:

package interfacedemo;

public class VehicleApp {
    interface Vehicle {
        void start();
    }

    static class Car implements Vehicle {
        public void start() {
            System.out.println("Car starts with key ignition or push button");
        }
    }

    static class Bike implements Vehicle {
        public void start() {
            System.out.println("Bike starts with kick or self start");
        }
    }

    public static void main(String[] args) {

        Vehicle v = new Car();
        v.start();

        Vehicle v1 = new Bike();
        v1.start();
    }
}
Enter fullscreen mode Exit fullscreen mode

βœ… Output:

Car starts with key ignition or push button
Bike starts with kick or self start
Enter fullscreen mode Exit fullscreen mode

πŸ’‘ Explanation:

πŸ‘‰ start() is common
πŸ‘‰ Implementation differs per vehicle


πŸ”Ή Q3: How Interface Helps in Notification System?

🧠 Scenario:

System sends:

  • Email
  • SMS
  • Push Notification

πŸ‘‰ Future expansion needed


βœ… Program:

package interfacedemo;

public class NotificationApp {

    interface Notification {
        void send(String message);
    }

    static class EmailNotification implements Notification {
        public void send(String message) {
            System.out.println("Email sent: " + message);
        }
    }

    static class SMSNotification implements Notification {
        public void send(String message) {
            System.out.println("SMS sent: " + message);
        }
    }

    static class PushNotification implements Notification {
        public void send(String message) {
            System.out.println("Push notification sent: " + message);
        }
    }

    public static void main(String[] args) {

        Notification n = new EmailNotification();
        n.send("Order placed successfully");

        Notification n1 = new SMSNotification();
        n1.send("OTP: 1234");

        Notification n2 = new PushNotification();
        n2.send("New offer available!");
    }
}
Enter fullscreen mode Exit fullscreen mode

βœ… Output:

Email sent: Order placed successfully
SMS sent: OTP: 1234
Push notification sent: New offer available!
Enter fullscreen mode Exit fullscreen mode

πŸ’‘ Explanation:

πŸ‘‰ Easy to extend β†’ WhatsApp / Slack later
πŸ‘‰ No need to change existing code


πŸ”Ή Q4: How Interface Helps in Team Development?

🧠 Scenario:

Multiple developers working on modules:

  • Login
  • Payment
  • Reports

πŸ‘‰ Need common structure


βœ… Program:

package interfacedemo;

public class TeamProject {

    interface Module {
        void execute();
    }

    static class LoginModule implements Module {
        public void execute() {
            System.out.println("Login Module Executed");
        }
    }

    static class PaymentModule implements Module {
        public void execute() {
            System.out.println("Payment Module Executed");
        }
    }

    static class ReportModule implements Module {
        public void execute() {
            System.out.println("Report Module Executed");
        }
    }

    public static void main(String[] args) {

        Module m = new LoginModule();
        m.execute();

        Module m1 = new PaymentModule();
        m1.execute();

        Module m2 = new ReportModule();
        m2.execute();
    }
}
Enter fullscreen mode Exit fullscreen mode

βœ… Output:

Login Module Executed
Payment Module Executed
Report Module Executed
Enter fullscreen mode Exit fullscreen mode

πŸ’‘ Explanation:

πŸ‘‰ Interface acts as contract
πŸ‘‰ All developers follow same method structure
πŸ‘‰ Ensures consistency


πŸ”₯ Key Advantages of Interface

βœ” Abstraction
βœ” Loose coupling
βœ” Scalability
βœ” Code reusability
βœ” Easy maintenance


πŸ”š Final Takeaway

πŸ‘‰ Interface is not just theory β€” it is used everywhere in real projects

From:

  • Payment systems πŸ’³
  • Notifications πŸ“©
  • Vehicles πŸš—
  • Team modules πŸ‘¨β€πŸ’»

πŸ‘‰ It helps in building clean and scalable applications πŸš€


πŸ’‘ Golden Insight

πŸ‘‰ "Interface defines WHAT to do, implementation defines HOW to do"

πŸ€– A Small Note
I used ChatGPT to help structure and refine this blog while ensuring the concepts remain aligned with my trainer’s explanations.

Top comments (0)