Next: Inherited methods and variables
Up: Unit 03
Previous: Constructor of a derived
In general, when the subclass has its own instance variables, its constructor
must first construct an object of the superclass (using super) and
then construct its own instance variables.
Note:
- What happens if we forget to insert super()? Then the
constructor with no arguments of the superclass is called automatically
(obviously, if the constructor with no arguments is not defined for the
superclass, then a compilation error is signaled).
- What happens if we forget to define constructors for the subclass? Then
a constructor with no arguments is automatically defined; such a constructor
calls the constructor with no arguments of the superclass and initializes the
proper (non inherited) instance variables of the subclass to the default
values.
- In this course, we will never make use of these automatic definitions.
Instead, we will always explicitly define the constructors of a subclass in
such a way that in their first statement they call super().
Next: Inherited methods and variables
Up: Unit 03
Previous: Constructor of a derived