Published on

Interfaces

Authors
  • avatar
    Name
    Iraianbu A
    Twitter

Table of Contents

Introduction

Interface is a crucial concept that defines a contract for classes to follow. It is a collection of abstract methods that a class must implement. It is used to achieve multiple inheritance in Java. Java introduces default and static methods in interfaces.

interface Flyable {
    void fly();
}

interface Drivable {
    void drive();
}

class FlyingCar implements Flyable, Drivable {
    @Override
    void fly() {
        System.out.println("Flying car is flying");
    }
    @Override
    void drive() {
        System.out.println("Flying car is driving");
    }
}

public class Main {
    public static void main(String... args) {
        FlyingCar car = new FlyingCar();
        car.fly();
        car.drive();
    }
}

Output

Flying car is flying
Flying car is driving

Real world Examples

interface Payment {
    void pay(double amount);
}

class CreditCardPayment implements Payment {
    @Override
    public void pay(double amount) {
        System.out.println("Paid " + amount + " using Credit Card");
    }
}

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

public class Main {
    public static void main(String... args) {
        Payment payment = new CreditCardPayment();
        payment.pay(100.0);
        payment = new UPI();
        payment.pay(200.0);
    }
}

Output

Paid 100.0 using Credit Card
Paid 200.0 using UPI

References