Multiple Interfaces Example
This program demonstrates multiple inheritance using interfaces.
Problem Statement
Write a Java program where SmartDevice implements both Camera and MusicPlayer interfaces.
Source Code
| 1 | interface Camera { void click(); } |
| 2 | interface MusicPlayer { void playMusic(); } |
| 3 | class SmartDevice implements Camera, MusicPlayer { |
| 4 | public void click(){ System.out.println("Eduinq Camera clicked photo"); } |
| 5 | public void playMusic(){ System.out.println("Eduinq Music is playing"); } |
| 6 | } |
| 7 | public class MultipleInterfacesExample { |
| 8 | public static void main(String[] args) { |
| 9 | System.out.println("Eduinq Multiple Interfaces Example"); |
| 10 | Camera c=new SmartDevice(); |
| 11 | MusicPlayer m=new SmartDevice(); |
| 12 | c.click(); m.playMusic(); |
| 13 | } |
| 14 | } |
Program Output
Eduinq Multiple Interfaces Example Eduinq Camera clicked photo Eduinq Music is playing
Explanation
Interfaces allow multiple inheritance in Java since classes can implement more than one interface. - SmartDevice implements both Camera and MusicPlayer, showing how one class can serve multiple roles. - Polymorphism allows the same object to be referenced as either Camera or MusicPlayer, enabling flexible design. - This is crucial in enterprise systems where devices or services often need to fulfill multiple responsibilities without violating single inheritance rules. Conclusion: Shows multiple inheritance via interfaces.