19.09.2015 Views

Prentice.Hall.Introduction.to.Java.Programming,.Brief.Version.9th.(2014).[sharethefiles.com]

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

11.3 Using the super Keyword 415<br />

The syntax <strong>to</strong> call a superclass’s construc<strong>to</strong>r is:<br />

super(), or super(parameters);<br />

The statement super() invokes the no-arg construc<strong>to</strong>r of its superclass, and the statement<br />

super(arguments) invokes the superclass construc<strong>to</strong>r that matches the arguments. The<br />

statement super() or super(arguments) must appear in the first line of the subclass’s<br />

construc<strong>to</strong>r; this is the only way <strong>to</strong> explicitly invoke a superclass construc<strong>to</strong>r. For example, the<br />

construc<strong>to</strong>r in lines 12–17 in Listing 11.2 can be replaced by the following code:<br />

public CircleFromSimpleGeometricObject(<br />

double radius, String color, boolean filled) {<br />

super(color, filled);<br />

this.radius = radius;<br />

}<br />

Caution<br />

You must use the keyword super <strong>to</strong> call the superclass construc<strong>to</strong>r, and the call must<br />

be the first statement in the construc<strong>to</strong>r. Invoking a superclass construc<strong>to</strong>r’s name in a<br />

subclass causes a syntax error.<br />

11.3.2 Construc<strong>to</strong>r Chaining<br />

A construc<strong>to</strong>r may invoke an overloaded construc<strong>to</strong>r or its superclass construc<strong>to</strong>r. If neither is<br />

invoked explicitly, the <strong>com</strong>piler au<strong>to</strong>matically puts super() as the first statement in the construc<strong>to</strong>r.<br />

For example:<br />

public ClassName() {<br />

// some statements<br />

}<br />

Equivalent<br />

public ClassName() {<br />

super();<br />

// some statements<br />

}<br />

public ClassName(double d) {<br />

// some statements<br />

}<br />

Equivalent<br />

public ClassName(double d) {<br />

super();<br />

// some statements<br />

}<br />

In any case, constructing an instance of a class invokes the construc<strong>to</strong>rs of all the superclasses<br />

along the inheritance chain. When constructing an object of a subclass, the subclass construc<strong>to</strong>r<br />

first invokes its superclass construc<strong>to</strong>r before performing its own tasks. If the superclass is<br />

derived from another class, the superclass construc<strong>to</strong>r invokes its parent-class construc<strong>to</strong>r<br />

before performing its own tasks. This process continues until the last construc<strong>to</strong>r along the<br />

inheritance hierarchy is called. This is called construc<strong>to</strong>r chaining.<br />

Consider the following code:<br />

construc<strong>to</strong>r chaining<br />

1 public class Faculty extends Employee {<br />

2 public static void main(String[] args) {<br />

3<br />

4<br />

new Faculty();<br />

}<br />

5<br />

6 public Faculty() {

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!