The Object Class

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:

All videos have a rental price, so the fixes should be made to the parent class Video. A new variable rent should be added to the parent class. Then modify its constructor and its show() method. The two child classes will inherit these changes. Fixing the parent class fixes all of its children.

The Object Class

Remember the rule: every constructor starts out with a super() constructor. If you don't explicitly put it in, then the Java compiler automatically puts it in for you. Now look at the definition of Video:

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 );
  } 
}

According to the rule, the compiler automatically does this:

  // constructor
  public Video( String ttl, int lngth )
  {
    super();     // use the super class's constuctor
    title = ttl; length = lngth; avail = true; 
  }

This is correct. All classes have a parent class (a super class) except one. The class at the top of the Java class hierarchy is called Object. If a class definition does not specify a parent class then it automatically has Object as a parent class. The compiler automatically assumes, for example:

class Video extends Object
{
. . .
}

QUESTION 22:

Is it possible to write a class definition that does not have Object as its ultimate ancestor?