Class implementing multiple interfaces
📖 Scenario: Imagine you are building a simple system to manage electronic devices. Each device can have different features like playing music and making calls. We want to organize these features using interfaces and then create a class that implements both.
🎯 Goal: Create two interfaces called MusicPlayer and Phone with specific methods. Then create a class called SmartDevice that implements both interfaces. Finally, create an instance of SmartDevice and call its methods to see the output.
📋 What You'll Learn
Create an interface called
MusicPlayer with a method playMusic() that returns void.Create an interface called
Phone with a method makeCall(number: string) that returns void.Create a class called
SmartDevice that implements both MusicPlayer and Phone.Inside
SmartDevice, implement playMusic() to print "Playing music...".Inside
SmartDevice, implement makeCall(number: string) to print "Calling {number}..." using the given number.Create an instance of
SmartDevice called myDevice.Call
playMusic() and makeCall("123-456-7890") on myDevice and print the outputs.💡 Why This Matters
🌍 Real World
In real life, devices often have multiple features. Using interfaces helps organize these features clearly and lets classes combine them as needed.
💼 Career
Understanding how to implement multiple interfaces is important for building flexible and maintainable code in TypeScript, a popular language for web and app development.
Progress0 / 4 steps