Computer Engineering is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

Interfaces in Java

Clear, exam-focused notes on Java interfaces: what they are, rules, syntax, default/static/functional features, examples, common mistakes and short practice questions.

Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.

Why it matters

Interfaces define a contract (a set of method signatures) separate from implementation. Engineers use interfaces to design modular, testable and loosely-coupled systems (for example, APIs, callbacks like Runnable, comparators, dependency injection), and to allow a form of multiple inheritance of type in Java.

Key ideas

  • Abstract method: a method declaration without a body. In an interface, an abstract method is implicitly public and abstract unless declared as default, static, or private (see below). Example form: int f(int x); (no body).

  • Implicit modifiers (exact university-style statements):

    • Every field declared in an interface is implicitly public static final. That is, int N = 10; inside an interface is equivalent to public static final int N = 10;. Such fields must be initialized at the point of declaration with a compile-time constant expression.
    • Every ordinary (non-default, non-static, non-private) method declared in an interface is implicitly public abstract.
    • These implicit modifiers hold regardless of whether you write them explicitly.
  • Default method (Java 8): a method in an interface declared with the default keyword that provides an implementation. Default methods let interfaces evolve without breaking existing implementors. Exact form: default void m() { ... }.

  • Static method in interface (Java 8): a method declared static inside an interface with a body. It is called on the interface name, not on instances. Exact form: static void helper() { ... } and called as InterfaceName.helper(). Static methods declared in interfaces are not inherited by implementing classes or subinterfaces; they must be called with the interface name.

  • Private methods in interfaces (Java 9): interfaces may contain private methods (and private static) to share code between default and static methods inside the interface. These private methods are not accessible to implementing classes.

  • Functional interface / SAM (single abstract method): an interface that has exactly one abstract method (ignoring default, static, and private methods). Such an interface can be implemented with a lambda expression. The optional @FunctionalInterface annotation documents this intent and causes a compile-time error if the interface no longer has exactly one abstract method. Methods declared in or inherited from java.lang.Object (e.g., equals, hashCode, toString) do not count toward the single abstract method.

  • Marker interface: an interface with no methods (empty body) used solely to mark or tag a class (for example Serializable). Modern Java often prefers annotations, but marker interfaces still exist.

  • Implementing classes:

    • A class implements an interface using the implements keyword: class C implements I { ... }.
    • A class that implements an interface must provide concrete implementations for all inherited abstract methods from the interface (and its parent interfaces), unless the class is declared abstract. The implementing methods must be declared public; you cannot reduce the visibility of an implemented interface method.
    • A class may implement multiple interfaces separated by commas: class C implements I1, I2 { ... }.
  • Interfaces extending interfaces: an interface may extend one or more interfaces: interface I extends J, K { ... }. This forms a type hierarchy for interfaces.

  • Multiple-inheritance-of-type and method resolution:

    • Java allows a class to implement multiple interfaces (multiple inheritance of type) but forbids multiple inheritance of implementation for classes.
    • If a superclass provides a concrete implementation of a method, it takes precedence over any default method with the same signature from an interface (class wins rule).
    • If two interfaces provide default methods with the same signature, a class that implements both must resolve the conflict by either overriding the method or explicitly invoking one default using InterfaceName.super.methodName(...) from the overriding method.
    • If two interfaces declare the same abstract method signature, a single concrete implementation in the class satisfies both; no conflict arises. However, if the two declarations have different return types that are not compatible (neither is a subtype of the other), a class cannot implement both interfaces.
  • Diagram to draw (if an exam asks for one): draw one rectangle labeled with the interface name (write <<interface>> I at top) and one rectangle labeled with the class name C. Draw a dashed line from the top-right of the class rectangle to the interface rectangle, ending in a hollow triangle arrow pointing to the interface. Label the dashed line implements. For multiple interfaces, draw multiple dashed lines to different interface rectangles. For interface extension, draw dashed lines with hollow triangle arrows from child interface boxes to parent interface boxes, labeled extends.

Assumptions behind rules: these rules assume Java language versions Java 8+ for default and static, and Java 9+ for private interface methods. If your syllabus is restricted to older Java (pre-8), default/static methods do not exist.

Formulas

interface InterfaceName { /* fields and method declarations */ } public class ClassName implements Interface1, Interface2 { /* must implement abstract methods */ } interface SubInterface extends Interface1, Interface2 { /* inherits abstract and default members (and constant fields); static interface methods are not inherited */ } default returnType methodName(params) { /* implementation */ } — default method inside interface static returnType methodName(params) { /* implementation */ } — static method inside interface @FunctionalInterface — marks a single-abstract-method interface for lambdas InterfaceName.staticMethod(...) — call a static method declared in an interface InterfaceName.super.methodName(...) — inside an instance method of a class that implements InterfaceName (or in a default method of a subinterface), call that interface's default method implementation

Worked examples

Example 1 — Simple interface with a constant and an abstract method (read radius, print area):

import java.util.Scanner;

interface Shape {
    String TYPE = "2D"; // implicitly public static final
    double area();       // implicitly public abstract
}

class Circle implements Shape {
    private double r;
    public Circle(double r) { this.r = r; }
    @Override
    public double area() { return Math.PI * r * r; }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        double r = sc.nextDouble();
        Circle c = new Circle(r);
        System.out.println("TYPE=" + TYPE);
        System.out.println("area=" + c.area());
    }
}

Sample input:

2

Exact output:

TYPE=2D
area=12.566370614359172

Example 2 — Default method conflict resolution and static method in interface:

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

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

class C implements A, B {
    // Resolve default method conflict by overriding
    @Override
    public void hello() {
        A.super.hello(); // call A's default
        System.out.println("Hello from C");
    }

    public static void main(String[] args) {
        C c = new C();
        c.hello();
        B.info(); // call static method on interface
    }
}

Exact output:

Hello from A
Hello from C
B static

Example 3 — Functional interface and lambda (reads two ints, prints their sum):

import java.util.Scanner;

@FunctionalInterface
interface Adder {
    int add(int a, int b);
}

class LambdaDemo {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        Adder ad = (x, y) -> x + y; // lambda implements the single abstract method
        System.out.println(ad.add(a, b));
    }
}

Sample input:

3 4

Exact output:

7

Note: all three examples are standard Java 8+ code (the private interface method feature is Java 9+ but is not used above).

Common mistakes

  • Forgetting that non-default methods in interfaces are implicitly public abstract, so writing a non-public method signature inside an interface is meaningless or causes confusion.
  • Trying to new an interface (cannot instantiate an interface directly).
  • Not implementing all inherited abstract methods in a concrete class — compile error.
  • Confusing extends and implements: extends is used for class-to-class inheritance and interface-to-interface extension; implements is used for class-to-interface.
  • Assuming multiple inherited default methods are chosen automatically — the class must resolve conflicts by overriding or using InterfaceName.super.method().
  • Thinking default methods provide state — they are just methods with bodies; interfaces still cannot have instance fields.

For your exam

Kinds of questions:

  • Definitions (define interface, default method, functional interface, marker interface).
  • Short notes (difference between abstract class and interface, implicit modifiers in interfaces).
  • Code-writing (write an interface and a class implementing it; show default/static usage; show resolving default-method conflicts).
  • Tracing code (given code with interfaces and classes, write the output or find the compile error).
  • Diagram/draw (class implements interface arrows; interface extends other interfaces).

What full-marks answers include:

  • For definitions: clear statement, bold or named term, and an example or short note about version (e.g., default methods exist from Java 8).
  • For code questions: correct syntax, correct use of implements/extends, correct method visibility and overriding, and correct output for sample input.
  • For conflict-resolution questions: show the overriding method and demonstrate use of InterfaceName.super.method() when needed.
  • If the syllabus restricts Java version, explicitly state version assumptions (important when default/static methods are part of the question).

Quick check

  1. In an interface, fields are implicitly which modifiers?
  2. Can a Java class implement more than one interface? If yes, how to list them?
  3. What is a functional interface (SAM)?

Answers: 1) public static final; 2) Yes — use class C implements I1, I2 { ... } with interfaces comma-separated; 3) An interface with exactly one abstract method (ignoring default/static/private methods), usable with lambda expressions. Methods of Object do not count toward the single abstract method.

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?