Yes.
Examine the following proposed constructor for Movie
.
It uses the setter methods of the parent class to initialize variables inherited from the parent.
The child has these setter methods by inheritance.
// proposed constructor public Movie( String ttl, int lngth, String dir, String rtng ) { setTitle( ttl ); // initialize inherited variables setLength( lngth ); setAvailable( true ); director = dir; // initialize Movie variables rating = rtng; }
It looks like this does not invoke a parent class 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 the following:
// proposed constructor public Movie( String ttl, int lngth, String dir, String rtng ) { super(); // inserted by compiler: parent's no-argument constructor setTitle( ttl ); // initialize inherited variables setLength( lngth ); setAvailable( 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.
(Review:) How can you write a class with a no-argument constructor?