Master Core Java Programming From Scratch

Clear, interactive, and structured coding lessons designed for absolute beginners.

Module 9

Modern Interfaces in Java

Learn default methods, static methods, private interface methods, functional interfaces, and modern interface design.

🔌

1. What is an Interface?

An interface in Java defines a contract that classes can implement. Interfaces are mainly used for abstraction, loose coupling, and multiple inheritance of type.

Java
interface CiitCourse {

    void registerStudent();
}

class JavaCourse implements CiitCourse {

    public void registerStudent() {
        System.out.println("Successfully enrolled in the CIIT Java Full Stack Developer program 😁👨‍🏫..!");
    }
}

The class implementing an interface must provide implementations for its required abstract methods.

2. Default Methods

A default method is a method inside an interface that contains an implementation.

Java
interface Vehicle {

    void start();

    default void stop() {
        System.out.println("Vehicle stopped");
    }
}

class Car implements Vehicle {

    public void start() {
        System.out.println("Car started");
    }
}

The implementing class automatically gets the default implementation unless it provides its own implementation.

3. Why Were Default Methods Introduced?

Default methods allow existing interfaces to evolve by adding new behavior without requiring every existing implementation to immediately implement the new method.

Key point: Default methods were especially important for evolving Java APIs while maintaining compatibility with existing implementations.

4. Overriding a Default Method

A class can provide its own implementation of a default interface method.

Java
interface Vehicle {

    default void display() {
        System.out.println("Vehicle");
    }
}

class Car implements Vehicle {

    public void display() {
        System.out.println("Car");
    }
}

5. Calling the Interface Default Method

When a class overrides a default method, it can still explicitly call the interface implementation using InterfaceName.super.methodName().

Java
interface Vehicle {

    default void display() {
        System.out.println("Vehicle");
    }
}

class Car implements Vehicle {

    public void display() {

        Vehicle.super.display();

        System.out.println("Car");
    }
}

6. Static Methods in Interfaces

Interfaces can contain static methods. These methods belong to the interface itself and are called using the interface name.

Java
interface MathUtility {

    static int square(int number) {
        return number * number;
    }
}

public class Main {

    public static void main(String[] args) {

        int result =
            MathUtility.square(5);

        System.out.println(result);
    }
}
Important: Static interface methods are accessed using the interface name.

7. Private Methods in Interfaces

Java 9 introduced private methods in interfaces. They are useful for sharing common implementation logic between default methods.

Java
interface Logger {

    default void logInfo(String message) {

        String formatted =
            format(message);

        System.out.println(formatted);
    }

    default void logError(String message) {

        String formatted =
            format(message);

        System.out.println(formatted);
    }

    private String format(String message) {

        return "[LOG] " + message;
    }
}

The private method is available only inside the interface. Implementing classes cannot directly call it.

8. Private Static Methods

Interfaces can also contain private static methods.

Java
interface Validator {

    static boolean isValid(String value) {

        return isNotEmpty(value)
            && hasMinimumLength(value);
    }

    private static boolean isNotEmpty(
        String value
    ) {

        return value != null
            && !value.isBlank();
    }

    private static boolean hasMinimumLength(
        String value
    ) {

        return value.length() >= 3;
    }
}

9. Variables in Interfaces

Fields declared inside an interface are implicitly public, static, and final.

Java
interface Configuration {

    int MAX_USERS = 100;
}

public class Main {

    public static void main(String[] args) {

        System.out.println(
            Configuration.MAX_USERS
        );
    }
}

10. Functional Interfaces

A functional interface contains exactly one abstract method. Functional interfaces are commonly used with lambda expressions and method references.

Java
interface Calculator {

    int calculate(int a, int b);
}

public class Main {

    public static void main(String[] args) {

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

        System.out.println(
            add.calculate(10, 20)
        );
    }
}

Java also provides the FunctionalInterface annotation as an optional compile-time check for functional interfaces.

11. Multiple Interface Implementation

A Java class can implement more than one interface.

Java
interface Printable {

    void print();
}

interface Showable {

    void show();
}

class Document
    implements Printable, Showable {

    public void print() {
        System.out.println("Printing");
    }

    public void show() {
        System.out.println("Showing");
    }
}

12. Default Method Conflict

If two interfaces provide default methods with the same signature, the implementing class must resolve the conflict.

Java
interface A {

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

interface B {

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

class Test implements A, B {

    public void display() {

        A.super.display();

        B.super.display();
    }
}

13. Abstract Method and Default Method Conflict

If one interface declares an abstract method and another interface provides a default method with the same signature, the implementing class must provide its own implementation.

Java
interface A {

    void display();
}

interface B {

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

class Test implements A, B {

    public void display() {
        System.out.println("Test");
    }
}

14. Interface Extending Another Interface

An interface can extend another interface.

Java
interface Animal {

    void eat();
}

interface Dog extends Animal {

    void bark();
}

class Labrador implements Dog {

    public void eat() {
        System.out.println("Eating");
    }

    public void bark() {
        System.out.println("Barking");
    }
}

15. Interface Extending Multiple Interfaces

An interface can extend multiple interfaces.

Java
interface Printable {

    void print();
}

interface Scannable {

    void scan();
}

interface MultiFunctionDevice
    extends Printable, Scannable {
}

class Printer
    implements MultiFunctionDevice {

    public void print() {
        System.out.println("Printing");
    }

    public void scan() {
        System.out.println("Scanning");
    }
}

16. Rules for Private Interface Methods

  • Private interface methods are implementation helpers.
  • They cannot be accessed by implementing classes.
  • They can be called from methods inside the same interface.
  • They can reduce duplicated logic.
  • They can be instance methods or static methods.

17. Modern Interface Design

A modern interface can contain abstract methods, default methods, static methods, and private helper methods.

Java
interface ReportService {

    void generate();

    default void printReport() {

        String report =
            createReport();

        System.out.println(report);
    }

    private String createReport() {

        return "Report generated";
    }

    static String version() {

        return "1.0";
    }
}

18. Example: Notification Service 🧑‍💻📩

Java
interface NotificationService {

    void send(String message);

    default void sendWelcomeMessage(
        String user
    ) {

        send(
            formatWelcomeMessage(user)
        );
    }

    private String formatWelcomeMessage(
        String user
    ) {

        return "Welcome, " + user;
    }
}

class EmailNotification
    implements NotificationService {

    public void send(String message) {

        System.out.println(
            "Email: " + message
        );
    }
}

19. Interface Evolution

Default methods make it possible to evolve interfaces while providing existing implementations with default behavior.

This is particularly useful in libraries and frameworks where many classes may already implement an interface.

20. Interface vs Abstract Class

Feature Interface Abstract Class
Multiple implementation Yes No, one direct superclass
Abstract methods Yes Yes
Default methods Yes No
Static methods Yes Yes
Private methods Yes Yes
Instance fields No Yes
Constructors No Yes

21. Advantages of Modern Interfaces

  • Provides abstraction.
  • Encourages loose coupling.
  • Supports multiple interface implementation.
  • Default methods allow interface evolution.
  • Private methods reduce duplicated implementation logic.
  • Static methods provide interface-specific utilities.
  • Works well with lambda expressions.
  • Useful for API and library design.

22. Limitations

  • Interfaces cannot maintain normal per-object instance state.
  • Too many default methods can make an interface difficult to understand.
  • Default method conflicts may require explicit resolution.
  • Interfaces are not a replacement for every abstract-class use case.

23. Best Practices

  • Keep interfaces focused on a clear responsibility.
  • Use default methods for sensible reusable behavior.
  • Use private methods to remove duplicated interface logic.
  • Use static methods for interface-related utilities.
  • Avoid putting excessive business logic into interfaces.
  • Use functional interfaces for small behavior contracts.
  • Follow interface segregation principles.

24. Interview Questions

A default method is an interface method that contains a concrete implementation using the default keyword.

Private methods in interfaces were introduced in Java 9.

Yes. Static interface methods are called using the interface name.

No. Static interface methods belong to the interface and are not inherited as overridable instance methods.

They allow common implementation logic to be reused by default and static methods without exposing helper methods to implementing classes.

Yes. A Java class can implement multiple interfaces.

Summary

In this lesson, you learned:

  • Modern Java interfaces
  • Default methods
  • Static methods
  • Private interface methods
  • Private static methods
  • Functional interfaces
  • Multiple interface implementation
  • Default method conflicts
  • Interface inheritance
  • Interface evolution
  • Modern interface design
  • Real-world interface usage