0
0
Javaprogramming~30 mins

Multiple inheritance using interfaces in Java - Mini Project: Build & Apply

Choose your learning style9 modes available
Multiple inheritance using interfaces
πŸ“– Scenario: Imagine you are building a simple system for a smart home. Different devices can perform different actions. Some devices can turn on lights, others can play music, and some can do both.
🎯 Goal: You will create two interfaces, LightControl and MusicControl, each with one method. Then, you will create a class SmartDevice that inherits from both interfaces and implements their methods. Finally, you will create an object of SmartDevice and call both methods.
πŸ“‹ What You'll Learn
Create an interface called LightControl with a method turnOnLight().
Create an interface called MusicControl with a method playMusic().
Create a class called SmartDevice that implements both LightControl and MusicControl.
Implement the methods turnOnLight() and playMusic() in SmartDevice.
Create a main method to create an object of SmartDevice and call both methods.
πŸ’‘ Why This Matters
🌍 Real World
Smart home devices often need to perform multiple actions. Using interfaces allows devices to inherit multiple behaviors cleanly.
πŸ’Ό Career
Understanding multiple inheritance with interfaces is important for designing flexible and reusable code in Java, a common skill in software development jobs.
Progress0 / 4 steps
1
Create interfaces for light and music control
Create an interface called LightControl with a method void turnOnLight(). Also create an interface called MusicControl with a method void playMusic().
Java
Need a hint?

Use the interface keyword to create interfaces. Define methods without a body.

2
Create SmartDevice class implementing both interfaces
Create a class called SmartDevice that implements both LightControl and MusicControl interfaces.
Java
Need a hint?

Use implements keyword to inherit from interfaces. Separate multiple interfaces with commas.

3
Implement methods in SmartDevice
Inside the SmartDevice class, implement the methods turnOnLight() and playMusic(). Make turnOnLight() print "Light is turned on" and playMusic() print "Music is playing".
Java
Need a hint?

Remember to add public before method names when implementing interface methods.

4
Create main method and test SmartDevice
Create a public static void main(String[] args) method inside a class called Main. Inside it, create an object of SmartDevice called device. Call device.turnOnLight() and device.playMusic().
Java
Need a hint?

Make sure to create the Main class with the main method. Then create the SmartDevice object and call both methods.