Appearance
Lesson 03 · Interfaces
Objectives
After this lesson you will be able to:
- Declare interfaces with abstract,
default,static, andprivatemethods. - 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(unlessdefault,static, orprivate). - 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
| Interface | Abstract class | |
|---|---|---|
| Multiple inheritance | Yes (implements many) | No (extends one) |
| Instance fields | No (constants only) | Yes |
| Constructors | No | Yes |
| Method bodies | default/static/private | any non-abstract method |
| State | none | yes |
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 arepublic static finalconstants. No instance fields, no constructors. - Bodies come via
default,static, orprivatemethods; defaults can't bestatic/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
What are the implicit modifiers on int MAX = 10; in an interface?
Two implemented interfaces both declare default String hi(). What must class C do?
Which is ILLEGAL in an interface?
How many classes can a class extend, and how many interfaces implement?
Can an interface declare a constructor or an instance field?
Next: Enums. Run the matching code in labs/src/main/java/com/jse21/m03_oop/.