Java Interfaces

An interface defines a contract.

It tells a class:

“If you implement me, you must provide these behaviors.”

The interface mainly defines what should happen, while the implementing class decides how it happens.

For example:

interface Payment {
    void pay(double amount);
}

Any class implementing Payment must provide the pay() method.

class UPI implements Payment {

    @Override
    public void pay(double amount) {
        System.out.println("Paid ₹" + amount + " using UPI");
    }
}

So:

Payment
   ↓
 Contract
   ↓
UPI / Card / Cash

Complete Example in One Java File

Create a file:

Main.java
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

interface Payment {

    // 1. Constant
    // Automatically: public static final
    double PLATFORM_FEE = 2.0;

    // 2. Abstract method
    // Automatically: public abstract
    void pay(double amount);

    // 3. Default method
    default void generateReceipt(double amount) {
        logTransaction(amount);

        System.out.println(
            "Receipt generated for ₹" + amount
        );
    }

    // 4. Static method
    static void paymentInfo() {
        System.out.println(
            "Payment system supports multiple payment methods."
        );
    }

    // 5. Private method
    private void logTransaction(double amount) {
        System.out.println(
            "Logging transaction of ₹" + amount
        );
    }
}

// Another interface
interface Refundable {

    void refund(double amount);

    default void status() {
        System.out.println("Refund supported");
    }
}

// UPI implements multiple interfaces
class UPI implements Payment, Refundable {

    @Override
    public void pay(double amount) {
        System.out.println(
            "Paid ₹" + amount + " using UPI"
        );
    }

    @Override
    public void refund(double amount) {
        System.out.println(
            "Refunded ₹" + amount + " to UPI"
        );
    }
}

// Card provides its own version of default method
class Card implements Payment {

    @Override
    public void pay(double amount) {
        System.out.println(
            "Paid ₹" + amount + " using Card"
        );
    }

    @Override
    public void generateReceipt(double amount) {
        System.out.println(
            "Card receipt generated for ₹" + amount
        );
    }
}

// Functional Interface
@FunctionalInterface
interface Calculator {

    int calculate(int a, int b);

    // Default methods are allowed
    default void info() {
        System.out.println("Calculator operation");
    }
}

// Default method conflict example

interface A {

    default void show() {
        System.out.println("A show()");
    }
}

interface B {

    default void show() {
        System.out.println("B show()");
    }
}

class Demo implements A, B {

    @Override
    public void show() {

        System.out.println("Resolving conflict");

        A.super.show();
        B.super.show();
    }
}

public class Main {

    public static void main(String[] args) {

        // ---------------------------------
        // 1. Basic Interface
        // ---------------------------------

        Payment upi = new UPI();

        upi.pay(1000);

        upi.generateReceipt(1000);

        System.out.println();

        // ---------------------------------
        // 2. Another Implementation
        // ---------------------------------

        Payment card = new Card();

        card.pay(2000);

        card.generateReceipt(2000);

        System.out.println();

        // ---------------------------------
        // 3. Static Interface Method
        // ---------------------------------

        Payment.paymentInfo();

        System.out.println();

        // ---------------------------------
        // 4. Interface Constant
        // ---------------------------------

        System.out.println(
            "Platform Fee: ₹" + Payment.PLATFORM_FEE
        );

        System.out.println();

        // ---------------------------------
        // 5. Multiple Interfaces
        // ---------------------------------

        UPI phonePe = new UPI();

        phonePe.pay(500);

        phonePe.refund(200);

        System.out.println();

        // ---------------------------------
        // 6. Functional Interface
        // ---------------------------------

        Calculator add = (a, b) -> a + b;

        Calculator multiply = (a, b) -> a * b;

        System.out.println(
            "Addition: " + add.calculate(10, 20)
        );

        System.out.println(
            "Multiplication: " + multiply.calculate(10, 20)
        );

        System.out.println();

        // ---------------------------------
        // 7. Built-in Functional Interfaces
        // ---------------------------------

        Predicate<Integer> isEven =
                number -> number % 2 == 0;

        Function<String, Integer> getLength =
                text -> text.length();

        Consumer<String> printMessage =
                message -> System.out.println(message);

        Supplier<String> getGreeting =
                () -> "Hello from Supplier";

        System.out.println(
            isEven.test(10)
        );

        System.out.println(
            getLength.apply("Java")
        );

        printMessage.accept(
            "Learning Java Interfaces"
        );

        System.out.println(
            getGreeting.get()
        );

        System.out.println();

        // ---------------------------------
        // 8. Default Method Conflict
        // ---------------------------------

        Demo demo = new Demo();

        demo.show();
    }
}

1. Abstract Methods

An interface can declare methods without providing their implementation.

interface Payment {

    void pay(double amount);
}