Using super in a Child's Method

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:

Yes — looks like we wrote the same code twice.

Using super in a Child's Method

Sometimes (as in the example) you want a child class to have its own method, but that method includes everything the parent's method does. You can use the super reference in this situation. For example, here is Video's method:

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

Here is Movie's method without using super:

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

Movie's method would better be written using super:

public void show()
{
  super.show();
  System.out.println( "dir: " + director + "  " + rating );  
}

Unlike the case when super is used in a constructor, inside a method super does not have to be used in the first statement.

QUESTION 16:

Think of two reasons why using super in this way is a good thing to do.