go to previous page   go to home page   go to next page

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;       // initialize inherited variables 
  length = lngth;  
  avail = true;
  
  director = dir;    // initialize Movie variables
  rating = rtng;
}

It looks like this does not invoke the parent class's constructor. All variables are initialized in this constructor including those variables defined in the parent class. However, a constructor from the parent class is always invoked even if you don't explicitly 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;      // initialize inherited variables 
  length = lngth;  
  avail = true;
  
  director = dir;  // initialize Movie variables
  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?