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
EasyLet'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()andpowerOff(), both returning void.VolumeControl.java: Define an interface for devices with volume control. It should declare a methodsetVolume(int level)that returns void, and a methodgetVolume()that returns an int.SmartTV.java: Create a class that implements bothPowerableandVolumeControlinterfaces. Your SmartTV should have abrandfield (String), anisOnfield (boolean, initially false), and avolumefield (int, initially 0). Include a constructor that takes the brand name. Implement all required methods:powerOn()should setisOnto true and print:[brand] TV is now ONpowerOff()should setisOnto false and print:[brand] TV is now OFFsetVolume(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]usinggetVolume() - Call
powerOff()
- Call
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()
}
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Object Oriented Programming
1Fundamentals of OOP
External FilesIntroduction to OOPClasses vs ObjectsThe this KeywordMethodsFields (Attributes)Constructor MethodConstructor OverloadingRecap - Simple Calculator4Inheritance
Basic Inheritance (extends)The super KeywordMethod Overriding (@Override)Constructor ChainingThe Object ClassSingle & Multilevel InheritWhy No Multi Class InheritRecap - Employee Hierarchy7Special Methods & Object Class
toString() Methodequals() and hashCode()clone() MethodcompareTo() and ComparableComparator InterfaceRecap - Custom Sorting2Access Modifiers & Encapsulate
Access Levels OverviewGetter and Setter MethodsInformation HidingThe final KeywordRecap - Bank Account Manager5Polymorphism
Method Overloading BasicsMethod Overriding (Run-Time)Upcasting and DowncastingThe instanceof OperatorAbstract Classes and MethodsRecap - Shape Calculator8Advanced OOP Concepts
Composition vs InheritanceAggregation vs CompositionInner Nested & Anonymous ClassEnums and Enum MethodsRecords (Java 16+)Sealed Classes (Java 17+)3Class Props & Static Member
Instance vs Static VariablesStatic MethodsStatic BlocksConstants (static final)Recap - Counter & Utility6Interfaces & Abstract Classes
Introduction to InterfacesImplementing InterfacesMulti Interface ImplemenDefault & Static in InterfaceAbstract Classes vs InterfacesFunctional InterfacesRecap - Payment System