Skip to content

Lesson 03 · Interfaces

Objectives

After this lesson you will be able to:

  • Declare interfaces with abstract, default, static, and private methods.
  • Understand implicit modifiers on interface members.
  • Resolve the diamond conflict when two interfaces supply the same default.
  • Tell an interface from an abstract class.

Interface members and implicit modifiers

An interface is a contract. Its members carry implicit modifiers you don't write:

  • Methods are implicitly public abstract (unless default, static, or private).
  • Fields are implicitly public static final — i.e. constants.
java
interface Shape {
    double PI = 3.14159;            // public static final
    double area();                 // public abstract

    default String describe() {    // has a body; instances inherit it
        return "area=" + area();
    }
    static Shape unit() { return () -> 1.0; }   // static factory, called Shape.unit()
    private double scaled() { return area() * 2; }   // private helper (Java 9+)
}

Exam trap

An interface cannot have instance fields or constructors — its fields are always public static final constants. A default method may not be static, final, or abstract, and you cannot mark an interface method protected.

Implementing and the diamond problem

A class implements one or more interfaces and must provide bodies for all abstract methods. If two interfaces provide the same default method, the implementing class must override it to resolve the conflict — optionally delegating with Interface.super.method().

java
interface A { default String hi() { return "A"; } }
interface B { default String hi() { return "B"; } }
class C implements A, B {
    @Override public String hi() {
        return A.super.hi() + B.super.hi();   // explicit choice → "AB"
    }
}

Gotcha

A class can implements many interfaces (multiple inheritance of type) but extends only one class. A class member always wins over an inherited default ("class wins" rule).

Interface vs abstract class

InterfaceAbstract class
Multiple inheritanceYes (implements many)No (extends one)
Instance fieldsNo (constants only)Yes
ConstructorsNoYes
Method bodiesdefault/static/privateany non-abstract method
Statenoneyes

Choose an interface for a capability many unrelated types can have; an abstract class when subclasses share state and a common base.

Beyond the exam

functional interface (exactly one abstract method) and lambdas are introduced here only in passing; Module 06 covers them in depth. Note Shape.unit() above returns a lambda implementing the single abstract area().

Key Takeaways

  • Interface methods are implicitly public abstract; fields are public static final constants. No instance fields, no constructors.
  • Bodies come via default, static, or private methods; defaults can't be static/ final/abstract.
  • A class may implement many interfaces; conflicting defaults must be overridden (resolve via Iface.super.m()). A concrete class method beats any default.
  • Prefer an interface for a shared capability, an abstract class for shared state + base.

Lesson Quiz

Lesson Quiz · Interfaces0 / 5
  1. What are the implicit modifiers on int MAX = 10; in an interface?

    • Apublic abstract
    • Bpublic static final
    • Cprivate final
    • Dprotected static
  2. Two implemented interfaces both declare default String hi(). What must class C do?

    • ANothing — picks one at random
    • BOverride hi() to resolve the conflict
    • CIt won't compile no matter what
    • DMark C abstract
  3. Which is ILLEGAL in an interface?

    • Adefault method
    • Bstatic method
    • Cprivate method
    • Dprotected method
  4. How many classes can a class extend, and how many interfaces implement?

    • A1 class, 1 interface
    • Bmany classes, 1 interface
    • C1 class, many interfaces
    • Dmany of each
  5. Can an interface declare a constructor or an instance field?

    • ABoth
    • BConstructor only
    • CInstance field only
    • DNeither

Next: Enums. Run the matching code in labs/src/main/java/com/jse21/m03_oop/.