Skip to content

Java OOP

0/36 lessons · 0% complete

— XP

Java OOP · Module 11

What is an interface?

interface Flyable { void fly(); } — and what `Flyable f = new Bird();` really means

26m60 XP
→→→

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
1

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

Main.java
1interface Flyable {
2 void fly();
3}
4
5class Bird implements Flyable {
6 @Override
7 public void fly() {
8 System.out.println("Bird is flying");
9 }
10}
interface Flyable {
interface instead of class. This type can never be instantiated — new Flyable() is impossible, exactly like an abstract class.
void fly();
A signature and a semicolon — no body. The compiler silently treats it as public abstract void fly();. You may write those words, but every Java style guide says do not: they are redundant.
class Bird implements Flyable {
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
Yes, you use @Override when implementing an interface method too — it is still an override of an inherited abstract method.
public void fly() {
`public` is mandatory here. Interface methods are implicitly public, and an override may never narrow access. Omitting public gives: *attempting to assign weaker access privileges; was public*.

`Flyable f = new Bird(); f.fly();` — the four questions

QuestionAnswerWhy
Type of the reference f?Flyable (an interface type)the declared type on the left
Type of the real object?Birdnew Bird() created it; no interface object exists
What can I call through f?only the methods declared in Flyablethe 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.

The exact example, running

Add `void land();` to the interface and re-run — Bird stops compiling until you implement it. That is the contract being enforced.

Output
Press Run to execute this example.

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

2

Interface Variables

Interface variables are constants

Three implicit modifiers
Main.java
1interface SpeedLimit {
2 int MAX_SPEED = 120; // really: public static final int MAX_SPEED = 120;
3 String UNIT = "km/h"; // really: public static final String UNIT = "km/h";
4}
5
6class Car implements SpeedLimit {
7 void check(int speed) {
8 if (speed > MAX_SPEED) { // inherited constant
9 System.out.println("Too fast! Limit is " + MAX_SPEED + " " + UNIT);
10 }
11 }
12}
13
14// SpeedLimit.MAX_SPEED = 200; ❌ final — cannot be reassigned
Because the fields are static and final, they belong to the interface itself and can never change. There is no such thing as per-object state in an interface.

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

Contract
The set of method signatures an interface requires.
Implementation
A class that fulfils the contract with real bodies.
Interface reference
A variable whose declared type is the interface: Flyable f = ....
Marker interface
An interface with no methods at all, used purely as a tag (for example Serializable).
Functional interface
Exactly one abstract method — the shape a lambda can satisfy in modern Java.

Common mistakes

Wrongclass Bird extends Flyable
Why`extends` is for classes; interfaces are attached with `implements`. (An interface may `extend` another *interface*, though.)
Fixclass Bird implements Flyable
Wrongvoid fly() { ... } in the implementing class, without `public`.
WhyInterface methods are implicitly public; package-private narrows the access, which is forbidden.
Fixpublic void fly() { ... }
WrongFlyable f = new Flyable();
WhyAn interface has no bodies and no constructor — there is nothing to create.
FixInstantiate an implementing class: `Flyable f = new Bird();`

Lesson playground

Free practice

One contract, two unrelated media types, one loop. Add a `Podcast` class and put it in the queue — the loop never changes.

Output
Press Run to execute this example.

Check understanding

Quiz

Question 1 of 5

Two classes implement one interface. What is printed?

Main.java
1interface Switchable {
2 void turnOn();
3}
4
5class Lamp implements Switchable {
6 @Override
7 public void turnOn() {
8 System.out.println("Lamp: light on");
9 }
10}
11
12class Fan implements Switchable {
13 @Override
14 public void turnOn() {
15 System.out.println("Fan: spinning");
16 }
17}
18
19public class Main {
20 public static void main(String[] args) {
21 Switchable[] devices = new Switchable[] { new Lamp(), new Fan() };
22
23 for (int i = 0; i < devices.length; i++) {
24 devices[i].turnOn();
25 }
26 }
27}

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.