This project demonstrates the use of polymorphism in Java with the concept of interfaces. It involves controlling media playback through a common interface (Playable), where both music and video players implement this interface to provide the functionality of playing their respective media.
- Playable Interface: This interface defines a
play()method, which is implemented by bothMusicPlayerandVideoPlayerclasses to play respective media. - MusicPlayer Class: Implements
Playableand provides an implementation for theplay()method, indicating that a music track is being played. - VideoPlayer Class: Implements
Playableand provides an implementation for theplay()method, indicating that a video with music is being played. - MediaController Class: A controller class that uses the
Playableinterface to manage and play media, regardless of whether it’s audio or video.
- Polymorphism: Both
MusicPlayerandVideoPlayerimplement the samePlayableinterface, and theMediaControllerclass can call theplay()method on any object that implementsPlayable. - Interface Implementation:
Playabledefines a contract (play()method), which is implemented by both media player classes. - Method Overriding: Both
MusicPlayerandVideoPlayeroverride theplay()method to provide specific implementations for music and video media types.
- MusicPlayer: Plays music when the
play()method is invoked. - VideoPlayer: Plays video and music together when the
play()method is invoked. - MediaController: Calls the
play()method onPlayableobjects (eitherMusicPlayerorVideoPlayer) to control media playback.
- Main.java: The entry point where the objects of
MusicPlayer,VideoPlayer, andMediaControllerare created and used to demonstrate the functionality. - Playable.java: The interface that defines the
play()method, implemented by bothMusicPlayerandVideoPlayer. - MusicPlayer.java: Implements the
Playableinterface and plays music. - MediaController.java: Uses the
Playableinterface to control and play media (either music or video). - VideoPlayer.java: Implements the
Playableinterface and plays video and music.
- Java 17+
- IDE (e.g., IntelliJ IDEA)