Skip to content

Interfaces — implements, Default/Static/Private Methods, and Functional Interfaces

DodaTech Updated 2026-06-28 7 min read

In this tutorial, you will learn about Interfaces. We cover key concepts, practical examples, and best practices to help you master this topic.

Java interfaces define contracts that classes implement, evolving beyond pure abstraction to include default, static, and private methods. An interface is a reference type that specifies a set of methods a class must implement — it establishes a contract without dictating implementation details.

What You'll Learn

  • Declaring and implementing interfaces
  • Default, static, and private methods (Java 8+ and Java 9+)
  • Multiple interface inheritance and Conflict Resolution
  • Functional interfaces for lambda expressions

Why It Matters

Interfaces are the foundation of Java's type system for abstraction. Frameworks like Spring, JPA, and JDBC all expose interfaces. Programming to interfaces makes code testable, flexible, and decoupled from specific implementations.

Real-World Use

List, Map, Set, Runnable, Callable, Comparable, Serializable — all interfaces. Spring repositories, JPA entity managers, and JDBC connections are all accessed through interfaces.


Declaring and Implementing Interfaces

public interface Drawable {
    void draw(); // implicitly public and abstract
}

public class Circle implements Drawable {
    @Override
    public void draw() {
        System.out.println("Drawing a circle");
    }
}
  • Interface methods are implicitly public abstract
  • Interface fields are implicitly public static final (constants)
  • A class can implement multiple interfaces: class A implements X, Y, Z
  • A class must implement all abstract methods or be declared abstract

Multiple Interfaces

public interface Printable {
    void print();
}

public interface Scannable {
    void scan();
}

public class MultiFunctionPrinter implements Printable, Scannable {
    @Override
    public void print() {
        System.out.println("Printing...");
    }

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

Default Methods (Java 8+)

Default methods provide a default implementation in the interface itself:

public interface Printable {
    void print();

    default void printTwice() {
        print();
        print();
    }
}

Classes can override default methods if needed:

public class FastPrinter implements Printable {
    @Override
    public void print() {
        System.out.println("Fast printing");
    }

    // Inherits printTwice() — no need to override
}

Why Default Methods?

Default methods allow adding new methods to existing interfaces without breaking all implementing classes. Before Java 8, adding a method to Collection would have broken every implementation. With default methods, the List interface could add sort(), replaceAll(), and spliterator() without breaking ArrayList, LinkedList, etc.

Static Methods in Interfaces (Java 8+)

public interface MathOperations {
    static boolean isPositive(int value) {
        return value > 0;
    }
}

boolean result = MathOperations.isPositive(5); // called on the interface

Static methods in interfaces are utility methods that belong to the interface, not to implementing classes. They cannot be inherited.

Private Methods in Interfaces (Java 9+)

Private methods share common code between default methods without exposing it:

public interface Logger {
    default void info(String msg) {
        log("INFO", msg);
    }

    default void error(String msg) {
        log("ERROR", msg);
    }

    private void log(String level, String msg) {
        System.out.println(level + ": " + msg);
    }
}

Private methods can also be static — used to share code between static methods.

Method Resolution (Diamond Problem)

When a class implements multiple interfaces with the same default method:

interface A {
    default void greet() {
        System.out.println("Hello from A");
    }
}

interface B {
    default void greet() {
        System.out.println("Hello from B");
    }
}

class C implements A, B {
    // Must override greet() to resolve conflict
    @Override
    public void greet() {
        A.super.greet(); // can call a specific parent
    }
}

Rules:

  1. Class wins: The class's explicit method declaration or override takes precedence
  2. Subinterface wins: If one interface extends another, the more specific default wins
  3. Conflict: If both are unrelated, the class must override

Functional Interfaces

A functional interface has exactly one abstract method:

@FunctionalInterface
public interface Runnable {
    void run();
}

@FunctionalInterface
public interface Comparator<T> {
    int compare(T o1, T o2);
    // plus default and static methods
}

The @FunctionalInterface annotation is optional but recommended — it triggers a compile error if the interface has more than one abstract method.

Functional interfaces enable lambda expressions:

Comparator<String> byLength = (s1, s2) -> Integer.compare(s1.length(), s2.length());

Java provides many built-in functional interfaces in java.util.function:

Interface Method Description
Predicate<T> boolean test(T) Tests a condition
Function<T,R> R apply(T) Transforms input to output
Consumer<T> void accept(T) Performs an action
Supplier<T> T get() Supplies a value without input
UnaryOperator<T> T apply(T) Function where input/output same type
BinaryOperator<T> T apply(T, T) Combines two values of same type

Marker Interfaces

Marker interfaces have no methods and convey metadata:

public interface Serializable { }
public interface Cloneable { }
public interface RandomAccess { }

Modern Java prefers annotations over marker interfaces (@Override, @FunctionalInterface).

Common Mistakes

  1. Forgetting @Override when implementing interface methods. Not required but catches typos and signature mismatches.
  2. Adding too many abstract methods to a functional interface. The @FunctionalInterface annotation enforces a single abstract method.
  3. Trying to instantiate an interface. new Drawable() does not compile — you need a concrete class or anonymous implementation.
  4. Assuming default methods are available on all JVM languages. Default methods require Java 8+. Some older Android versions do not support them.
  5. Using protected or private on interface methods (before Java 9). Interface methods are public by default. Private methods in interfaces require Java 9+.

Practice Questions

1. What is the difference between an abstract class and an interface?
Abstract classes can have fields, constructors, and any access modifier. Interfaces (pre-Java 8) were pure contracts. Since Java 8, interfaces can have default and static methods, but still cannot hold state or have constructors.

2. Can a class implement multiple interfaces?
Yes. Java supports multiple inheritance of type (interfaces), but single inheritance of implementation (classes).

3. What problem do default methods solve?
They allow adding new methods to existing interfaces without breaking all implementing classes. This enabled the Java 8 collections API enhancements.

4. What is a functional interface?
An interface with exactly one abstract method. Used as the target for lambda expressions. Examples: Runnable, Callable, Comparator, Predicate.

5. How does Java resolve conflicting default methods?
If a class inherits conflicting default methods from two interfaces, the class must override the method. It can delegate to a specific parent using InterfaceName.super.methodName().

Challenge Question:
Design a Playable interface with a default method play() that prints "Playing..." and an abstract method pause(). Create a MediaPlayer interface that extends Playable and adds stop(). Implement AudioPlayer and VideoPlayer. Add a Skippable interface with default method skip(). Resolve any method conflicts.

FAQ

Can an interface extend another interface?

Yes. Interfaces use extends (not implements): interface B extends A { }. An interface can extend multiple interfaces: interface C extends A, B { }.

What is the difference between `abstract class` and `interface` after Java 8?

The lines have blurred. Both can have default implementations. The main differences: abstract classes can have state (fields), constructors, and non-public methods. Interfaces are limited to constants, and instance methods are implicitly public.

Can I define a `final` method in an interface?

Yes, a default method can be final (Java 9+), preventing implementing classes from overriding it. This is useful for invariant methods that should not be changed.

Can an interface have instance fields?

No. All fields in an interface are implicitly public static final. Interfaces cannot hold mutable instance state. This is by design — interfaces define contracts, not implementation.

What is the `@FunctionalInterface` annotation?

An annotation that indicates an interface is intended to be a functional interface. The compiler checks that the interface has exactly one abstract method. It is optional but recommended.

Mini Project

Write a program InterfaceDemo.java that:

  1. Defines a Drawable interface with void draw() and a default method void display() that calls draw() and prints "Displayed"
  2. Defines a Resizable interface with void resize(double factor) and a static method Resizable scaleToFit(double width, double height) that returns a default resize factor
  3. Creates Rectangle and Circle that implement both interfaces
  4. Demonstrates default method inheritance, static method call, and conflict resolution
  5. Creates a functional interface ShapeFactory with method Shape create(double... params) and uses it with a lambda

What's Next

Interfaces define contracts, but where do these contracts live? Lesson 17 covers packages and imports — how Java organizes classes into packages, the import statement, package naming conventions, and the Java Platform Module System (JPMS) introduced in Java 9.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro