๐ŸŽ‰ Special Offer !    Code: GET300OFF    Flat โ‚น300 OFF on every Java Course
Grab Deal ๐Ÿš€

Interface in Java  


Introduction
  • An interface in Java is a blueprint of a class, containing only method signatures (no implementations) and constants.
  • Interfaces are similar to abstract class but having all the methods of abstract type.
  • Syntax :-
    interface InterfaceName
    {
    	// public static final variables (constants)
    	// public abstract methods
    }

Use of Interfaces :-
  1. Used to achieve 100% abstraction in Java.
  2. Used to define a common behaviour across unrelated classes.
  3. Used to achieve multiple inheritance in Java.
  4. Used to achieve loose coupling in our code.
  5. Used extensively in frameworks, APIs, and design patterns (e.g., DAO, Service Layer).
1. Used to achieve 100% abstraction in Java.
  • Example:
    // Interface with 100% abstraction
    interface Vehicle
    {
        void start();
        void stop();
    }
    
    // Car class implements the interface
    class Car implements Vehicle
    {
        public void start()
        {
            System.out.println("Car is starting...");
        }
    
        public void stop()
        {
            System.out.println("Car is stopping...");
        }
    }
    
    // Main class to test
    public class Main
    {
        public static void main(String[] args)
        {
            Vehicle v = new Car();  // Interface reference (polymorphism)
            v.start();
            v.stop();
        }
    }
    Output:
    Car is starting...
    Car is stopping...
2. Used to define a common behaviour across unrelated classes.
  • Example:
    // Interface with a common method
    interface Printable
    {
        void print();
    }
    
    // Unrelated class 1
    class Document implements Printable
    {
        public void print()
        {
            System.out.println("Printing document...");
        }
    }
    
    // Unrelated class 2
    class Image implements Printable
    {
        public void print()
        {
            System.out.println("Printing image...");
        }
    }
    
    // Main class
    public class Main
    {
        public static void main(String[] args)
        {
            Printable p1 = new Document();
            Printable p2 = new Image();
    
            p1.print();
            p2.print();
        }
    }
    Output:
    Printing document...
    Printing image...
3. Used to achieve multiple inheritance in Java.
  • Example:
    interface I1
    {
        void m1();
    }
    
    interface I2
    {
        void m2();
    }
    
    // Multiple Inheritance using interfaces
    class A implements I1, I2
    {
        public void m1()
        {
            System.out.println("Method m1 from interface I1");
        }
    
        public void m2()
        {
            System.out.println("Method m2 from interface I2");
        }
    }
    
    // Main class
    public class Main
    {
        public static void main(String[] args)
        {
            A obj = new A();
            obj.m1();
            obj.m2();
        }
    }
    Output:
    Method m1 from interface I1
    Method m2 from interface I2
4. Used to achieve loose coupling in our code.
  • Example:
    import java.util.Scanner;
    
    // Interface to achieve loose coupling
    interface Payment
    {
        void pay();
    }
    
    // UPI Payment Implementation
    class UpiPayment implements Payment
    {
        public void pay()
        {
            System.out.println("Payment done using UPI.");
        }
    }
    
    // Net Banking Payment Implementation
    class NetBankingPayment implements Payment
    {
        public void pay()
        {
            System.out.println("Payment done using Net Banking.");
        }
    }
    
    // Checkout class using interface (not tightly bound to any one payment method)
    class PaymentCheckout
    {
        void payment(Payment payment)
        {
            payment.pay();  // Loose coupling: works with any class that implements Payment
        }
    }
    
    // Main class with switch-case
    public class MainApp
    {
        public static void main(String[] args)
        {
            Scanner scanner = new Scanner(System.in);
            PaymentCheckout checkout = new PaymentCheckout();
    
            System.out.println("Choose payment method:");
            System.out.println("1. UPI");
            System.out.println("2. Net Banking");
            System.out.print("Enter choice: ");
            int choice = scanner.nextInt();
    
            switch (choice)
            {
                case 1:
                    checkout.payment(new UpiPayment());
                    break;
                case 2:
                    checkout.payment(new NetBankingPayment());
                    break;
                default:
                    System.out.println("Invalid choice");
            }
        }
    }
    Output:
    If user enters 1:
    Choose payment method:
    1. UPI
    2. Net Banking
    Enter choice: 1
    Payment done using UPI.
    If user enters 2:
    Choose payment method:
    1. UPI
    2. Net Banking
    Enter choice: 2
    Payment done using Net Banking.
5. Used extensively in frameworks, APIs, and design patterns
  • Frameworks:
    • Spring uses interfaces like CrudRepository, ApplicationContext.
    • Helps in writing clean, loosely-coupled code.
  • Java APIs:
    • Collections use interfaces like List, Set, Map.
    • JDBC provides interfaces like Connection, Statement.
  • Design Patterns:
    • DAO Pattern: Interface defines data access methods.
    • Service Layer: Interface declares business logic methods.
  • Testing:
    • Interfaces are mocked in unit tests for flexibility and isolation.