Course topics

By WebNest Studio Engineering Team

Java - Core Tutorial

Interfaces in Java

An interface in Java is a fully abstract contract: it defines a set of methods a class promises to implement, without dictating how. Interfaces are declared with the interface keyword, and a class agrees to that contract using the implements keyword.

Modern Java interfaces are more flexible than they used to be. Beyond plain abstract method signatures, interfaces can now include default methods, static methods, and constants — while still never holding instance state of their own.

Why it matters

An interface describes what a class can do without saying how, and a class can implement as many as it needs. That is how unrelated classes are treated alike — anything Comparable can be sorted, anything Runnable can be run on a thread. Almost every Java framework is built around interfaces, so reading and designing them is an everyday skill.

Watch: Java Interfaces Explained in Depth | Default, Static, Private & Functional Interfaces

A walkthrough of Java interfaces: defining contracts, implementing interfaces, and using default and static methods.

Watch on YouTube

Interface Members

Every field declared in an interface is implicitly public static final, whether or not you write those modifiers — interfaces cannot hold per-instance state. Methods without a body are implicitly public abstract.

  • Abstract methods — no body; any implementing class must provide one.
  • Default methods — declared with the default keyword and a body, providing a fallback implementation that implementing classes may override.
  • Static methods — declared with static and called on the interface itself, e.g. Comparator.naturalOrder().
  • Constants — fields are always public static final automatically.

Implementing Multiple Interfaces

Java classes can only extend one superclass, but they can implement any number of interfaces, separated by commas: class Duck implements Flyable, Swimmable. This is how Java achieves a safe form of "multiple inheritance of type" without the ambiguity problems C++ faces with multiple inheritance of implementation.

Default Methods and Diamond Conflicts

If a class implements two interfaces that each declare a default method with the same signature, the compiler forces the class to override that method explicitly and resolve the conflict — it will not guess which default to use. This keeps default methods from silently reintroducing the classic diamond problem.

Example

An interface with an abstract method, a default method, and a static method

Java
interface Vehicle {
    int MAX_SPEED = 180; // implicitly public static final

    void start(); // implicitly public abstract

    default void honk() {
        System.out.println("Beep beep!");
    }

    static Vehicle basic() {
        return () -> System.out.println("Basic vehicle starting");
    }
}

public class InterfaceDemo implements Vehicle {
    @Override
    public void start() {
        System.out.println("Vehicle starting, max speed " + MAX_SPEED);
    }

    public static void main(String[] args) {
        InterfaceDemo v = new InterfaceDemo();
        v.start();
        v.honk();
        Vehicle.basic().start();
    }
}
Output
Vehicle starting, max speed 180
Beep beep!
Basic vehicle starting

Common Mistakes

  • Trying to declare an instance field in an interface expecting per-object state — all interface fields are implicitly static and shared.
  • Forgetting that interface methods without a body are implicitly public, then marking one "protected" or "private" by mistake and hitting a compile error.
  • Not resolving a default method conflict when a class implements two interfaces that share a default method signature.
  • Confusing "implements" (for interfaces) with "extends" (for classes) — a class extends at most one class but implements as many interfaces as needed.

Exercise

Define an interface Payable with an abstract method amount() and a default method receipt() that returns "Paid " followed by the amount. Then write an Invoice class that implements it.

Expected output: Paid 1200

Java · starter code
// TODO: interface Payable with int amount() and a default String receipt()

// TODO: class Invoice implements Payable, constructed with a total

public class PayableDemo {
    public static void main(String[] args) {
        Payable invoice = new Invoice(1200);
        System.out.println(invoice.receipt());
    }
}
Show hints
  • A default method has a body and is written with the default keyword inside the interface.
  • An implementing method must be public, because interface methods are public.
Show solution
Java · solution
interface Payable {
    int amount();

    default String receipt() {
        return "Paid " + amount();
    }
}

class Invoice implements Payable {
    private final int total;

    Invoice(int total) {
        this.total = total;
    }

    @Override
    public int amount() {
        return total;
    }
}

public class PayableDemo {
    public static void main(String[] args) {
        Payable invoice = new Invoice(1200);
        System.out.println(invoice.receipt()); // Paid 1200
    }
}

Quick quiz

1. A field declared inside an interface is implicitly what?

  • A. private
  • B. protected and static
  • C. public, static and final
  • D. An instance variable
Show answer

C. Interface fields are always constants: public, static and final, whether or not you write those modifiers.

2. A class implements two interfaces that both define a default method with the same signature. What must the class do?

  • A. Nothing; the first interface wins
  • B. Mark one interface as primary
  • C. Override the method itself, or the code does not compile
  • D. Remove one of the interfaces
Show answer

C. The compiler cannot choose between the two defaults, so the class must override the method and may call one with InterfaceName.super.method().

3. Can you create an interface instance with new Payable() on its own?

  • A. No; you need an implementing class, an anonymous class or a lambda
  • B. Yes
  • C. Only if it has default methods
  • D. Only if it has no methods
Show answer

A. An interface has no constructor. new Payable() { ... } works only because the braces define an anonymous implementing class.

Interview questions

Why were default methods added to interfaces in Java 8?

Show answer

To let interfaces evolve without breaking existing code. Before Java 8, adding a method to an interface forced every implementing class to be changed. A default method supplies a body in the interface itself, so existing implementations keep working. That is how methods such as forEach and stream were added to the collection interfaces.

What is a functional interface?

Show answer

An interface with exactly one abstract method. It may also have default and static methods. Because there is only one method to implement, an instance can be written as a lambda expression or a method reference. Runnable, Comparator and Predicate are examples, and the @FunctionalInterface annotation asks the compiler to enforce the rule.

Key Points to Remember

  • Interface fields are always public static final; abstract methods are always public abstract.
  • default and static methods let interfaces carry real behavior, not just signatures.
  • A class can implement multiple interfaces, enabling safe multiple inheritance of type.
  • Conflicting default methods from two interfaces must be resolved explicitly in the implementing class.

Practice in the playground

Write and run your own code for this topic, or start from the exercise or an example. Runs plain Java 17. Frameworks such as Spring, and databases, are not available here. Your code for each topic is kept in this browser.

Run the examples

Change an input, predict the result, then compare it with the output. Explain why the result changes.

WebNest Studio

Written by

WebNest Studio Engineering Team

Practical lessons from developers working with Java, Python, React, databases and production systems.

Led by Vansh Duggal, Founder, WebNest Studio · Full-stack & software engineering