Writing the Constructor

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.

Answer:

MusicVideo inherits title, length, and avail from its parent and adds artist and category.

Writing the Constructor

Notice that MusicVideo inherits only from its parent Video. It does not inherit anything from its sibling class Movie. Here is the definition so far:

class Video
{
  String  title;    // name of the item
  int     length;   // number of minutes
  boolean avail;    // is the video in the store?

  // constructor
  public Video( String ttl, int lngth )
  {
    title = ttl; length = lngth; avail = true; 
  }

  public void show()
  {
    System.out.println( title + ", " + length + " min. available:" + avail );
  }
  
}

class MusicVideo extends Video
{
  String artist;
  String category;

  // The constructor will go here

  // The show() method will go here

}

We need a constructor for MusicVideo. Use four parameters for title, length, artist and category. Initialize avail to true.

QUESTION 19:

Write a constructor for MusicVideo. Use the super reference to the constructor in Video.