Stop 05 · Inheritance, Polymorphism & InterfacesTopics · 4/9

Interfaces

Part of the Java Roadmap

In short

Interfaces are how big Java systems stay flexible. Your checkout code talks to a PaymentMethod contract, and UPI, cards or wallets can plug in without the checkout ever changing.

An interface defines a contract of methods a class promises to implement. A class can implement many interfaces, and interfaces can include default and static methods since Java 8.

The jaadu plugs a table fan, a mixer grinder and a phone charger into a row of identical wall sockets on a kitchen wall.

Think of it like this

The standard plug socket

Every wall socket in your home follows the same shape. A fan, a phone charger and a mixer grinder come from different companies, but all fit because they follow the socket's contract. The wiring doesn't care what is plugged in. An interface is that socket shape: any class that implements it can be plugged in.

Watch it happen

Checkout talks only to the contract

Checkout
PaymentMethod
UpiPayment

pay(499)

1/3Checkout holds a PaymentMethod reference. It knows only the contract.

The big ideas

What you need to know

A contract of methods

An interface lists what a class can do, not how. Its abstract methods are implicitly public and abstract.

Implement many

class UpiPayment implements PaymentMethod, Refundable is fine. A class can implement any number of interfaces.

default and static methods

Since Java 8, interfaces can ship default methods with a body, so adding a method doesn't break existing classes.

Use it as a type

Declare variables and parameters as PaymentMethod, not UpiPayment. Then any implementation can be passed in.

See it in code

Walk through the code

java
1interface PaymentMethod {2    boolean pay(long amountInRupees);3 4    String name();5 6    default String receipt(long amountInRupees) {7        return "Paid Rs " + amountInRupees + " via " + name();8    }9}10 11interface Refundable {12    void refund(long amountInRupees);13}14 15class UpiPayment implements PaymentMethod, Refundable {16    private final String upiId;17 18    UpiPayment(String upiId) {19        this.upiId = upiId;20    }21 22    @Override23    public boolean pay(long amountInRupees) {24        System.out.println("Collecting Rs " + amountInRupees + " from " + upiId);25        return true;26    }27 28    @Override29    public String name() {30        return "UPI";31    }32 33    @Override34    public void refund(long amountInRupees) {35        System.out.println("Refunding Rs " + amountInRupees + " to " + upiId);36    }37}

1/4Two abstract methods. No body, no access modifier needed: they are public by default.

PaymentMethod p = new UpiPayment("ananya@okbank"); if (p.pay(499)) System.out.println(p.receipt(499));

Save yourself the pain

Common mistakes

Oops

Implementing an interface method without public, like boolean pay(long a) { ... }.

Do this instead

Interface methods are public, and an implementation cannot reduce access. Mark it public.

Oops

Declaring variables with the concrete type everywhere: UpiPayment p = new UpiPayment(...).

Do this instead

Use the interface type, PaymentMethod p = ..., so the code works with any implementation.

Oops

Adding instance fields to an interface to share state.

Do this instead

Fields in an interface are always public static final constants. Put shared state in a class.

Your mission

Plug in a second payment method

~25 min

Quick check

Which statement about Java interfaces is correct?

Go deeper

  1. 1Create a Payable interface implemented by two unrelated classes.
  2. 2Add a default method to an interface.
  3. 3Compare when to use an interface versus an abstract class.

Also part of these roadmaps

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge