The document discusses the execution order of constructors in a class hierarchy, emphasizing that superclasses' constructors are executed before subclasses'. It illustrates this with examples of shipping costs and class inheritance, highlighting the reuse of code through inheritance. Additionally, it notes that while the classes can be shown in one file for convenience, they are typically organized into separate files in practice.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
2 views
java_research_4
The document discusses the execution order of constructors in a class hierarchy, emphasizing that superclasses' constructors are executed before subclasses'. It illustrates this with examples of shipping costs and class inheritance, highlighting the reuse of code through inheritance. Additionally, it notes that while the classes can be shown in one file for convenience, they are typically organized into separate files in practice.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5
The output of this program is shown here:
Volume of shipment1 is 3000.0
Weight of shipment1 is 10.0 Shipping cost: $3.41
Volume of shipment2 is 24.0
Weight of shipment2 is 0.76 Shipping cost: $1.28
Because of inheritance, Shipment can make use of the previously
defined classes of Box and BoxWeight, adding only the extra information it needs for its own, specific application. This is part of the value of inheritance; it allows the reuse of code. This example illustrates one other important point: super( ) always refers to the constructor in the closest superclass. The super( ) in Shipment calls the constructor in BoxWeight. The super( ) in BoxWeight calls the constructor in Box. In a class hierarchy, if a superclass constructor requires arguments, then all subclasses must pass those arguments “up the line.” This is true whether or not a subclass needs arguments of its own. Note In the preceding program, the entire class hierarchy, including Box, BoxWeight, and Shipment, is shown all in one file. This is for your convenience only. In Java, all three classes could have been placed into their own files and compiled separately. In fact, using separate files is the norm, not the exception, in creating class hierarchies.
When Constructors Are Executed
When a class hierarchy is created, in what order are the constructors for the classes that make up the hierarchy executed? For example, given a subclass called B and a superclass called A, is A’s constructor executed before B’s, or vice versa? The answer is that in a class hierarchy, constructors complete their execution in order of derivation, from superclass to subclass. Further, since super( ) must be the first statement executed in a subclass’ constructor, this order is the same whether or not super( ) is used. If super( ) is not used, then the default or parameterless constructor of each superclass will be executed. The following program illustrates when constructors are executed: The output from this program is shown here: Inside A's constructor Inside B's constructor Inside C's constructor
As you can see, the constructors are executed in order of derivation.
If you think about it, it makes sense that constructors complete their execution in order of derivation. Because a superclass has no knowledge of any subclass, any initialization it needs to perform is separate from and possibly prerequisite to any initialization performed by the subclass. Therefore, it must complete its execution first.