Java OOP · Module 11
What is an interface?
interface Flyable { void fly(); } — and what `Flyable f = new Bird();` really means
What you'll be able to do
- Declare an interface and implement it with `implements`
- Explain every implicit modifier the compiler adds inside an interface
- Dissect `Flyable f = new Bird(); f.fly();` reference-by-reference
- Know why interface variables are constants and why that is rarely useful
Interfaces
A contract, not a family
A Bird, an Airplane and a Drone share nothing as *things* — but all three can fly. Inheritance cannot express that. An interface can.
An interface is a named list of method signatures with no bodies: a pure contract. A class that writes implements Flyable promises 'I provide every method Flyable declares' — and the compiler holds it to that promise.
Why not just use an abstract class?
- A class may extend only one class, but implements any number of interfaces. Capabilities are not a single family tree.
- An interface describes what you can do, not what you are. Bird IS-A Animal (inheritance); Bird CAN-DO fly (interface).
- Completely unrelated classes can share an interface. Airplane is not an animal, yet both it and Bird are Flyable.
So the reading is: extends = *is a kind of*; implements = *is capable of*.
A driving licence
A licence does not say what species you are or who your parents were. It certifies one capability: you can drive. A taxi driver, a surgeon and a student can all hold the same licence, and any car-rental desk accepts all three — it only checks the certificate, never the person's biography. implements Drivable is that licence.
The contract and one signer
interface instead of class. This type can never be instantiated — new Flyable() is impossible, exactly like an abstract class.public abstract void fly();. You may write those words, but every Java style guide says do not: they are redundant.implements (not extends). From this moment Bird must provide fly() or fail to compile. Bird may still extend a class as well — both are allowed together.@Override when implementing an interface method too — it is still an override of an inherited abstract method.public gives: *attempting to assign weaker access privileges; was public*.`Flyable f = new Bird(); f.fly();` — the four questions
| Question | Answer | Why |
|---|---|---|
Type of the reference f? | Flyable (an interface type) | the declared type on the left |
| Type of the real object? | Bird | new Bird() created it; no interface object exists |
What can I call through f? | only the methods declared in Flyable | the compiler checks the *reference* type |
Which body runs for f.fly()? | Bird.fly() | dynamic dispatch uses the *object's* class |
Identical to `Animal a = new Dog();` from Module 8 — the only difference is that the stable type on the left is a contract instead of a parent class.
Add `void land();` to the interface and re-run — Bird stops compiling until you implement it. That is the contract being enforced.
What the compiler adds for you
Inside an interface:
- every method without a body is public abstract;
- every field is public static final (a constant);
- there is no constructor, and no instance state at all.
That last point is the real difference from an abstract class: an interface can promise behaviour but cannot store data.
Prove it — coding challenges
Interface Variables
Interface variables are constants
The constant-interface anti-pattern
Implementing an interface *only* to inherit its constants is considered bad practice: implements becomes a lie — it advertises a capability the class does not have. Put shared constants in a final class with static fields (or an enum) and refer to them as Config.MAX_SPEED.
Vocabulary you will hear
Flyable f = ....Serializable).Common mistakes
class Bird extends Flyablevoid fly() { ... } in the implementing class, without `public`.Flyable f = new Flyable();Lesson playground
One contract, two unrelated media types, one loop. Add a `Podcast` class and put it in the queue — the loop never changes.
Check understanding
Quiz
Question 1 of 5
Two classes implement one interface. What is printed?
Summary
- An interface is a pure contract: method signatures, no bodies, no instance state.
- `implements` = *is capable of*; `extends` = *is a kind of*. A class may implement many interfaces.
- Interface methods are implicitly `public abstract`; implementations must be declared `public`.
- `Flyable f = new Bird();` — the interface limits what you may call; the object decides what runs.