Example: The following example demonstrates a multi-level hierarchy.
1 class Box
2 { private double width, height, depth; //data hiding
3 double volume()
4 { double vol = width * height * depth;
5 return vol;
6 }
7 Box(double w, double h, double d)
8 { width = w; height = h; depth = d;
9 }
10 Box()
11 { width = -1; height = -1; depth = -1;
12 }
13 Box(double len)
14 { width = height = depth = len;
15 }
16 Box(Box ob)
17 { width = ob.width; height = ob.height; depth = ob.depth;
18 }
19 }
1 class BoxWeight extends Box
2 { double weight;
3 BoxWeight(BoxWeight ob)
4 { super(ob);
5 weight = ob.weight;
6 }
7 BoxWeight(double w, double h, double d, double m)
8 { super(w,h,d); weight = m;
9 }
10 BoxWeight()
11 { super(); weight = -1;
12 }
13 BoxWeight(double len, double m)
14 { super(len); weight = m;
15 }
16 }
1 class Shipment extends BoxWeight
2 {
3 double cost;
4 Shipment(Shipment ob)
5 { super(ob); cost = ob.cost;
6 }
7 Shipment(double w, double h, double d, double m, double c)
8 { super(w,h,d,m); cost = c;
9 }
10 Shipment()
11 { super(); cost = -1;
12 }
13 Shipment(double len, double m, double c)
14 { super(len,m); cost = c;
15 }
16 }
1 class ShipmentDemo
2 { public static void main(String args[])
3 { Shipment s = new Shipment(10,20,15,34.3,1000);
4 double vol = s.volume();
5 System.out.println(vol);
6 System.out.println(s.weight);
7 System.out.println(s.cost);
8 }
9 }
Output:
3000.0
34.3
1000.0
This example illustrates one important point: super() always refers to the constructor in the immediate super class. The super() in shipment calls the constructor in BoxWeight. The super() in BoxWeight calls the constructor in Box.
In a class hierarchy if a super class constructor requires parameters, then all subclasses must pass those parameters “up the line”. This is true whether or not a subclass needs parameters of its
own.
1. Order in which Constructors are called
When a class hierarchy is created, in what order are the constructors for the classes that make up the hierarchy called? The answer is that in a class hierarchy, constructors are called in order of derivation, from super class to subclass.
Further, since super() must be the first statement executed in a subclass’s constructor; this order is the same whether or not super() is used. If super() is not used, then the default or parameter less constructor of each super class will be executed.
Example: The following example illustrates the order in which constructors are called when child class object is created in case of a multi-level hierarchy.
1 class A
2 { A()
3 { System.out.println("A's Constructor");
4 }
5 }
1 class B extends A
2 { B()
3 { System.out.println("B's Constructor");
4 }
5 }
1 class C extends B
2 { C()
3 { System.out.println("C's Constructor");
4 }
5 }
1 class ConstOrderDemo
2 { public static void main(String args[])
3 { C c = new C();
4 }
5 }
Output:
A's Constructor
B's Constructor
C's Constructor
As you can see constructors are called in order of derivation. It makes sense that constructors are executed in order of derivation. Because a super class has no knowledge of any sub class, any initialization it needs to perform is separate and possibly pre-requisite to any initialization performed by the sub class. Therefore it must be executed first.
1 class Box
2 { private double width, height, depth; //data hiding
3 double volume()
4 { double vol = width * height * depth;
5 return vol;
6 }
7 Box(double w, double h, double d)
8 { width = w; height = h; depth = d;
9 }
10 Box()
11 { width = -1; height = -1; depth = -1;
12 }
13 Box(double len)
14 { width = height = depth = len;
15 }
16 Box(Box ob)
17 { width = ob.width; height = ob.height; depth = ob.depth;
18 }
19 }
1 class BoxWeight extends Box
2 { double weight;
3 BoxWeight(BoxWeight ob)
4 { super(ob);
5 weight = ob.weight;
6 }
7 BoxWeight(double w, double h, double d, double m)
8 { super(w,h,d); weight = m;
9 }
10 BoxWeight()
11 { super(); weight = -1;
12 }
13 BoxWeight(double len, double m)
14 { super(len); weight = m;
15 }
16 }
1 class Shipment extends BoxWeight
2 {
3 double cost;
4 Shipment(Shipment ob)
5 { super(ob); cost = ob.cost;
6 }
7 Shipment(double w, double h, double d, double m, double c)
8 { super(w,h,d,m); cost = c;
9 }
10 Shipment()
11 { super(); cost = -1;
12 }
13 Shipment(double len, double m, double c)
14 { super(len,m); cost = c;
15 }
16 }
1 class ShipmentDemo
2 { public static void main(String args[])
3 { Shipment s = new Shipment(10,20,15,34.3,1000);
4 double vol = s.volume();
5 System.out.println(vol);
6 System.out.println(s.weight);
7 System.out.println(s.cost);
8 }
9 }
Output:
3000.0
34.3
1000.0
This example illustrates one important point: super() always refers to the constructor in the immediate super class. The super() in shipment calls the constructor in BoxWeight. The super() in BoxWeight calls the constructor in Box.
In a class hierarchy if a super class constructor requires parameters, then all subclasses must pass those parameters “up the line”. This is true whether or not a subclass needs parameters of its
own.
1. Order in which Constructors are called
When a class hierarchy is created, in what order are the constructors for the classes that make up the hierarchy called? The answer is that in a class hierarchy, constructors are called in order of derivation, from super class to subclass.
Further, since super() must be the first statement executed in a subclass’s constructor; this order is the same whether or not super() is used. If super() is not used, then the default or parameter less constructor of each super class will be executed.
Example: The following example illustrates the order in which constructors are called when child class object is created in case of a multi-level hierarchy.
1 class A
2 { A()
3 { System.out.println("A's Constructor");
4 }
5 }
1 class B extends A
2 { B()
3 { System.out.println("B's Constructor");
4 }
5 }
1 class C extends B
2 { C()
3 { System.out.println("C's Constructor");
4 }
5 }
1 class ConstOrderDemo
2 { public static void main(String args[])
3 { C c = new C();
4 }
5 }
Output:
A's Constructor
B's Constructor
C's Constructor
As you can see constructors are called in order of derivation. It makes sense that constructors are executed in order of derivation. Because a super class has no knowledge of any sub class, any initialization it needs to perform is separate and possibly pre-requisite to any initialization performed by the sub class. Therefore it must be executed first.