Menu
Coddy logo textTech

Multi Interface Implemen

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

Unlike class inheritance where you can only extend one class, Java allows a class to implement multiple interfaces. This is how Java achieves a form of multiple inheritance safely.

To implement multiple interfaces, list them after the implements keyword, separated by commas:

public interface Drawable {
    void draw();
}

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

public class Rectangle implements Drawable, Resizable {
    private int width, height;
    
    public Rectangle(int width, int height) {
        this.width = width;
        this.height = height;
    }
    
    @Override
    public void draw() {
        System.out.println("Drawing rectangle " + width + "x" + height);
    }
    
    @Override
    public void resize(int width, int height) {
        this.width = width;
        this.height = height;
    }
}

The class must implement all methods from every interface it declares. You can also combine inheritance with multiple interfaces:

public class Square extends Shape implements Drawable, Resizable {
    // Must implement methods from both interfaces
    // Can also use inherited members from Shape
}

This flexibility lets you design classes that fulfill multiple roles. A SmartPhone could implement Callable, Messageable, and Browsable interfaces, each defining different capabilities the phone supports.

challenge icon

Challenge

Easy

Let's build a smart device system where devices can have multiple capabilities. You'll create a SmartTV that can be both powered on/off and have its volume controlled—demonstrating how a single class can implement multiple interfaces to fulfill different roles.

You'll organize your code across four files:

  • Powerable.java: Define an interface for devices that can be powered. It should declare two methods: powerOn() and powerOff(), both returning void.
  • VolumeControl.java: Define an interface for devices with volume control. It should declare a method setVolume(int level) that returns void, and a method getVolume() that returns an int.
  • SmartTV.java: Create a class that implements both Powerable and VolumeControl interfaces. Your SmartTV should have a brand field (String), an isOn field (boolean, initially false), and a volume field (int, initially 0). Include a constructor that takes the brand name. Implement all required methods:
    • powerOn() should set isOn to true and print: [brand] TV is now ON
    • powerOff() should set isOn to false and print: [brand] TV is now OFF
    • setVolume(int level) should update the volume and print: [brand] TV volume set to [level]
    • getVolume() should return the current volume
  • Main.java: Bring everything together to demonstrate multiple interface implementation. You'll receive two inputs: a brand name (String) and a volume level (int). Create a SmartTV with the given brand, then:
    • Call powerOn()
    • Call setVolume() with the given level
    • Print: Current volume: [volume] using getVolume()
    • Call powerOff()

You will receive two inputs: the TV brand (String) and the volume level (int).

Your output should show four lines demonstrating how your SmartTV fulfills both interface contracts—it can be powered like any Powerable device and have its volume controlled like any VolumeControl device!

Cheat sheet

A class can implement multiple interfaces by listing them after the implements keyword, separated by commas:

public class Rectangle implements Drawable, Resizable {
    // Must implement all methods from both interfaces
}

The class must implement all methods from every interface it declares.

You can also combine class inheritance with multiple interface implementation:

public class Square extends Shape implements Drawable, Resizable {
    // Implements methods from both interfaces
    // Can use inherited members from Shape
}

This allows a class to fulfill multiple roles and have different capabilities.

Try it yourself

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        // Read inputs
        String brand = scanner.nextLine();
        int volumeLevel = scanner.nextInt();
        
        // TODO: Create a SmartTV with the given brand
        
        // TODO: Call powerOn()
        
        // TODO: Call setVolume() with the given level
        
        // TODO: Print: Current volume: [volume] using getVolume()
        
        // TODO: Call powerOff()
        
    }
}
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