No-argument 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:

Yes.

No-argument Constructor

Examine the following proposed constructor for Movie:

// proposed constructor
public Movie( String ttl, int lngth, String dir, String rtng )
{
  title = ttl;       // do what the parent's constuctor does.
  length = lngth;  
  avail = true;
  
  director = dir; rating = rtng;
}

It looks like there is no need to invoke the parent class's constructor since all variables are initialized in this one. However a constructor from the parent class is always invoked even if you don't explicity ask for it. The Java compiler regards the above code as "shorthand" for this:

// proposed constructor
public Movie( String ttl, int lngth, String dir, String rtng )
{
  super();           // invoke the parent's no-argument constructor
  title = ttl;       // do what the parent's constuctor does.
  length = lngth;  
  avail = true;
  
  director = dir; rating = rtng;
}

As always, super() comes first, even if you don't write it in. If the parent does not have a no-argument constructor, then using this "shorthand" causes a syntax error.

In our program the class definition for Video (the superclass) lacks a no-argument constructor. The proposed constructor (above) calls for such a constructor so it would cause a syntax error.

QUESTION 13:

(Review:) When does a class have a no-argument constructor?