Lesson 5 of 518 minModule progress 0%

Module 6: Inheritance and Polymorphism

Interfaces

Use interfaces to define a shared capability that many unrelated classes can implement.

Author

Java Learner Editorial Team

Reviewer

Technical review by Java Learner

Last reviewed

2026-04-16

Java version

Java 25 LTS

How this lesson was prepared: AI-assisted draft, edited by hand, and checked against current Java 25 documentation and runnable examples.

Learning goals

  • Understand interfaces as contracts for behavior
  • Use `implements` in a class definition
  • See how interfaces differ from abstract classes

An interface defines what a class can do, not how it stores state: It is a contract for behavior.

Classes use implements to agree to that contract: A class must provide the methods required by the interface.

Interfaces are great for shared capabilities across unrelated classes: A Bird and an Airplane can both be Flyable even though they are not in the same family tree.

Simple difference from abstract classes: Abstract classes are strong parent templates; interfaces are behavior contracts.

Runnable examples

Two different classes can share one capability

interface Playable {
    void play();
}

class Song implements Playable {
    @Override
    public void play() {
        System.out.println("Playing song");
    }
}

class Video implements Playable {
    @Override
    public void play() {
        System.out.println("Playing video");
    }
}

Expected output

Different classes can follow the same `Playable` contract.

Mini exercise

Create a `Chargeable` interface and implement it in a `Phone` class.

Summary

  • Interfaces define a capability contract.
  • Classes use `implements` to adopt that contract.
  • Interfaces are useful even when classes are otherwise unrelated.

Next step

The next module moves back to data handling with arrays, `ArrayList`, and collection basics.

Sources used

Advertisement

Lesson check

What keyword does a class use to adopt an interface?

Next lesson →