Multiple Interfaces with Polymorphism
This program demonstrates multiple inheritance and polymorphism using interfaces.
Problem Statement
Write a Java program where SmartDevice implements both Camera and MusicPlayer interfaces.
Source Code
| 1 | interface Camera { |
| 2 | void click(); |
| 3 | } |
| 4 | interface MusicPlayer { |
| 5 | void playMusic(); |
| 6 | } |
| 7 | class SmartDevice implements Camera,MusicPlayer { |
| 8 | public void click(){ System.out.println("Camera clicked photo"); } |
| 9 | public void playMusic(){ System.out.println("Music is playing"); } |
| 10 | } |
| 11 | public class MultipleInterfacesPolymorphism { |
| 12 | public static void main(String[] args) { |
| 13 | System.out.println("Eduinq Multiple Interfaces Polymorphism"); |
| 14 | Camera c=new SmartDevice(); |
| 15 | MusicPlayer m=new SmartDevice(); |
| 16 | c.click(); m.playMusic(); |
| 17 | } |
| 18 | } |
Program Output
Eduinq Multiple Interfaces Polymorphism Camera clicked photo Music is playing
Explanation
SmartDevice implements two interfaces, achieving multiple inheritance. - Polymorphism allows SmartDevice to be referenced as either Camera or MusicPlayer. - This demonstrates how interfaces provide flexible design, enabling objects to behave differently depending on reference type. - It models real world scenarios where one object can serve multiple roles. Conclusion: Shows how multiple interfaces and polymorphism work together.