Search⌘ K
AI Features

Solution: Media Player Interfaces

Explore how to implement media player interfaces in Java by defining contracts with interfaces and creating abstract and concrete classes. Learn to use inheritance and polymorphism to enable flexible handling of audio and video media types through shared behaviors.

We'll cover the following...
Java 25
interface Playable {
void play();
}
abstract class MediaFile {
protected String title;
protected int duration; // Duration in seconds
public MediaFile(String title, int duration) {
this.title = title;
this.duration = duration;
}
}
class MP3File extends MediaFile implements Playable {
public MP3File(String title, int duration) {
super(title, duration);
}
@Override
public void play() {
System.out.println("Playing audio: " + title);
}
}
class MP4File extends MediaFile implements Playable {
public MP4File(String title, int duration) {
super(title, duration);
}
@Override
public void play() {
System.out.println("Playing video: " + title);
}
}
public class Main {
public static void main(String[] args) {
// Duration passed as seconds (e.g., 300 seconds = 5 mins)
Playable song = new MP3File("Jazz Classics", 300);
Playable movie = new MP4File("Action Movie", 5400);
song.play();
movie.play();
}
}
...