Java Interface Tutorial – A Complete Guide

In Java, an interface is a blueprint for a class. It is a reference type, similar to a class, and it can contain abstract methods, default methods, static methods, and constants. Interfaces are used to achieve abstraction and multiple inheritance in Java.

1. What is an Interface in Java?

An interface is a collection of abstract methods and constants. Unlike classes, interfaces cannot be instantiated. Classes implement interfaces to provide concrete behavior.

Key points:

  • Defines what a class should do, not how it does it.
  • Promotes loose coupling between classes.
  • Supports multiple inheritance (a class can implement multiple interfaces).

2. Syntax of an Interface

interface InterfaceName {
    // constant declaration
    int MAX_VALUE = 100;  // public, static, final by default

    // abstract method
    void method1();       // public and abstract by default

    // default method (since Java 8)
    default void defaultMethod() {
        System.out.println("This is a default method.");
    }

    // static method (since Java 8)
    static void staticMethod() {
        System.out.println("This is a static method.");
    }
}

3. Implementing an Interface

A class implements an interface using the implements keyword. It must override all abstract methods of the interface.

Example:

interface Vehicle {
    void start();
    void stop();
}

class Car implements Vehicle {
    @Override
    public void start() {
        System.out.println("Car is starting...");
    }

    @Override
    public void stop() {
        System.out.println("Car is stopping...");
    }
}

public class InterfaceDemo {
    public static void main(String[] args) {
        Vehicle myCar = new Car();
        myCar.start();
        myCar.stop();
    }
}

Output:

Car is starting...
Car is stopping...

4. Multiple Interface Implementation

Java allows a class to implement multiple interfaces, which is a way to achieve multiple inheritance.

interface Engine {
    void startEngine();
}

interface GPS {
    void navigate();
}

class SmartCar implements Engine, GPS {
    @Override
    public void startEngine() {
        System.out.println("Engine started!");
    }

    @Override
    public void navigate() {
        System.out.println("Navigating to destination...");
    }
}

public class MultipleInterfaceDemo {
    public static void main(String[] args) {
        SmartCar car = new SmartCar();
        car.startEngine();
        car.navigate();
    }
}

Output:

Engine started!
Navigating to destination...

5. Default and Static Methods in Interface

  • Default Methods: Allow interfaces to have method implementations.
  • Static Methods: Belong to the interface, not to the object.
interface Printer {
    void print();

    default void printPreview() {
        System.out.println("Printing preview...");
    }

    static void staticMethod() {
        System.out.println("Static method in interface.");
    }
}

class DocumentPrinter implements Printer {
    @Override
    public void print() {
        System.out.println("Printing document...");
    }
}

public class DefaultStaticDemo {
    public static void main(String[] args) {
        DocumentPrinter dp = new DocumentPrinter();
        dp.print();
        dp.printPreview();       // default method
        Printer.staticMethod();  // static method
    }
}

Output:

Printing document...
Printing preview...
Static method in interface.

6. Key Features of Interfaces

FeatureDescription
Abstract MethodsMethods without body (implemented by class).
Default MethodsMethods with default implementation.
Static MethodsBelongs to interface, not objects.
ConstantsVariables are public, static, final by default.
Multiple InheritanceA class can implement multiple interfaces.

7. Practical Example: Real-World Scenario

Imagine an online payment system. Different payment methods (CreditCard, PayPal) can implement the Payment interface.

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 PayPalPayment implements Payment {
    @Override
    public void pay(double amount) {
        System.out.println("Paid " + amount + " using PayPal.");
    }
}

public class PaymentDemo {
    public static void main(String[] args) {
        Payment payment1 = new CreditCardPayment();
        Payment payment2 = new PayPalPayment();

        payment1.pay(500);
        payment2.pay(1000);
    }
}

Output:

Paid 500.0 using Credit Card.
Paid 1000.0 using PayPal.

8. Interface vs Abstract Class

AspectInterfaceAbstract Class
MethodsAbstract by default, can have default & static methodsCan have abstract & concrete methods
Variablespublic, static, finalAny access modifier, non-final allowed
InheritanceMultiple interfaces allowedOnly single inheritance
Use caseDefine contract for unrelated classesShare code between closely related classes

9. Best Practices

  1. Use interfaces to define contracts for classes.
  2. Prefer interfaces over abstract classes when you need multiple inheritance.
  3. Keep interfaces focused: one interface = one responsibility.
  4. Use default methods sparingly; they are mostly for backward compatibility.

Conclusion:


Interfaces are fundamental in Java for abstraction, multiple inheritance, and loose coupling. Understanding them helps in designing flexible and maintainable systems.

Java developer with 9+ years of IT experience, sharing tutorials and tips to help learners master Java programming.

Leave a Reply

Your email address will not be published. Required fields are marked *