Java Reference
In-Depth Information
4.
If the parent class is also a child of another class, the parent class con-
structor must invoke a constructor in its parent before executing any
statements. Again, this is done using the super keyword.
5.
This process continues until we reach the top of the hierarchy tree,
which must be the Object class.
6.
The constructor in the Object class executes, then the flow of control
returns to the constructor in the class just below Object in the inheri-
tance hierarchy tree.
7.
The constructors execute their way down the hierarchy tree. The last
constructor to execute is actually the one that was invoked first in
Step 1.
Let's look at an example. Suppose we have the following BigScreenTV class
that extends the following Television class, and Television extends Object.
public class Television
{
public int channel, volume;
public Television()
{
this(4,5);
System.out.println(“Inside Television()”);
}
public Television(int c, int v)
{
System.out.println(“Inside Television(int, int)”);
channel = c;
volume = v;
}
}
public class BigScreenTV extends Television
{
public String aspectRatio;
public short size;
public BigScreenTV()
{
super();
aspectRatio = “unknown”;
size = 40;
System.out.println(“Inside BigScreenTV()”);
}
}
Notice that the BigScreenTV constructor explicitly invokes the no-argument
constructor in Television using the statement:
super();
Search WWH ::




Custom Search