Media Library System in Java: Explanation & Practice
Create a media type hierarchy
Problem summary
Create Media base class with Book, Movie, Song. Each has different play() behavior.
Starter code
// Media: title, play() abstract
// Book: play() prints "Reading: title"
// Movie: play() prints "Watching: title"
// Song: play() prints "Playing: title"
public class Main {
public static void main(String[] args) {
// Create array of Media, call play() on each
}
}Expected output and test cases
- All media types
Reading: 1984 Watching: Inception Playing: Bohemian
- Book only
Reading: Java Guide
- Movie only
Watching: Matrix
Hints
- Abstract Media class with abstract play()
- Each subclass implements play()
- Polymorphism calls correct method
Related Inheritance exercises
- Practice Simple Factory Pattern in Java
- Practice Simple Decorator in Java
- Practice Notification System in Java
Practice all Inheritance exercises · Run this idea in the Java compiler