Menu
Coddy logo textTech

Introduction to Interfaces

Part of the Object Oriented Programming section of Coddy's Java journey — lesson 34 of 87.

You've seen how abstract classes define contracts that subclasses must follow. But what if you want a class to follow multiple contracts? Java doesn't allow a class to extend multiple classes, but it solves this problem with interfaces.

An interface is a contract that defines what a class can do, without specifying how it does it. Unlike abstract classes, interfaces contain only method signatures (by default) with no implementation:

public interface Drawable {
    void draw();
}

public interface Resizable {
    void resize(int width, int height);
}

Think of an interface as a promise. When a class implements an interface, it promises to provide implementations for all the methods declared in that interface. The interface defines the capability, and the class provides the behavior.

Interfaces are declared using the interface keyword instead of class. All methods in an interface are implicitly public and abstract, so you don't need to write those keywords:

public interface Playable {
    void play();    // implicitly public abstract
    void pause();
    void stop();
}

Interfaces can also declare constants, which are implicitly public static final:

public interface GameConstants {
    int MAX_PLAYERS = 4;  // implicitly public static final
}

In the next lesson, you'll learn how classes implement interfaces using the implements keyword.

challenge icon

Challenge

Easy

Let's build a device control system by defining interfaces that describe what different devices can do. This challenge focuses on creating interfaces—the contracts that define capabilities without specifying how they work.

You'll organize your code across three files:

  • Switchable.java: Create an interface that defines what it means for a device to be switchable. Any switchable device should be able to turn on and turn off. Declare two methods: turnOn() and turnOff(). Remember, interface methods are implicitly public and abstract, so you just need the method signatures.
  • Adjustable.java: Create another interface that defines what it means for a device to be adjustable. An adjustable device should be able to set its level. Declare a method setLevel(int level). Also include a constant MAX_LEVEL with a value of 100—remember that constants in interfaces are implicitly public static final.
  • Main.java: Demonstrate that your interfaces are properly defined by printing information about them. You'll receive one input: a level value (int). Print the following three lines:
    • Switchable methods: turnOn, turnOff
    • Adjustable methods: setLevel
    • Max level constant: [MAX_LEVEL value] (access the constant from the Adjustable interface)
    Then print: Setting level to: [input level]

You will receive one input: a level value (int).

This challenge focuses purely on defining interfaces—the blueprints that describe capabilities. In the next lesson, you'll learn how classes actually implement these interfaces to provide the real behavior!

Cheat sheet

An interface is a contract that defines what a class can do, without specifying how it does it. Interfaces contain only method signatures with no implementation.

Interfaces are declared using the interface keyword:

public interface Drawable {
    void draw();
}

All methods in an interface are implicitly public and abstract:

public interface Playable {
    void play();    // implicitly public abstract
    void pause();
    void stop();
}

Interfaces can declare constants, which are implicitly public static final:

public interface GameConstants {
    int MAX_PLAYERS = 4;  // implicitly public static final
}

A class can implement multiple interfaces, unlike extending multiple classes.

Try it yourself

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int level = scanner.nextInt();
        
        // TODO: Print information about the interfaces
        // Print: "Switchable methods: turnOn, turnOff"
        // Print: "Adjustable methods: setLevel"
        // Print: "Max level constant: " followed by Adjustable.MAX_LEVEL
        // Print: "Setting level to: " followed by the input level
        
    }
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Object Oriented Programming