Using super to Call Superclass Constructors

A subclass can call a constructor defined by its superclass by use of the following form of super:

super(parameter-list);

Here, parameter-list specifies any parameters needed by the constructor in the superclass. super( ) must always be the first statement executed inside a subclass constructor. To see how super( ) is used, consider the version of TwoDShape in the following program. It defines a constructor that initializes width and height.

Image

Image

Here, Triangle( ) calls super( ) with the parameters w and ...

Get Java, A Beginner's Guide, 5th Edition, 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.