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

  1. Abstract Media class with abstract play()
  2. Each subclass implements play()
  3. Polymorphism calls correct method

Related Inheritance exercises

Practice all Inheritance exercises · Run this idea in the Java compiler