In the previous example, class BoxWeight derived from Box was not implemented as efficiently or as robustfully as it could have been.
For example, the constructor for BoxWeight explicitly initializes the width, height, and depth fields of Box(). Not only does this duplicate code of its super class, which is in-efficient, but it implies that a subclass must be granted access to these members.
However, there will be times when you will want to create a super class that keeps the details of its implementation to itself (that is, it keeps its data members private). In this case, there would be no way for a subclass to directly access or initialize these variables on its own.
Since encapsulation is a primary attribute of OOP, it is not surprising that Java provides a as solution to this problem.
Whenever a subclass needs to refer to its immediate super class, it can do so by use of keyword super.
The super has two general uses:
1. Using super to call super class constructors
A subclass can call a constructor method defined by its super class by use of the following form of super:
super(parameter-list);
Here, parameter-list specifies any parameters needed by the constructor in the super class. In fact super() must always be the first statement executed inside a subclass’s constructor.
To see how super() is used, consider the improved version of the BoxWeight() class in the following example.
Example:
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(double w, double h, double d, double m)
4 { super(w,h,d);
5 weight = m;
6 }
7 }
1 class BoxWeightDemo1
2 { public static void main(String args[])
3 { BoxWeight bw1 = new BoxWeight(10,20,15,34.3);
4 double vol;
5 vol = bw1.volume();
6 System.out.println(vol);
7 System.out.println(bw1.weight);
8 Box b1 = bw1;
9 vol = b1.volume();
10 System.out.println(vol);
11 //System.out.println(b1.weight); //will not compile
12 //BoxWeight bw2 = b1; //will not compile
13 BoxWeight bw2 = (BoxWeight) b1;
14 vol = bw2.volume();
15 System.out.println(vol);
16 System.out.println(bw2.weight);
17 Box b2 = new Box(5);
18 BoxWeight bw3 = (BoxWeight) b2;
19 System.out.println(bw3.weight);
20
21 }
22 }
Output:
3000.0
34.3
3000.0
3000.0
34.3
Exception in thread "main" java.lang.ClassCastException: Box
at BoxWeightDemo1.main(BoxWeightDemo1.java:18)
Here, BoxWeight() constructor calls super() with the parameters w, h, d. This causes the Box(double w, double h, double d) constructor to be called, which initializes width, height and depth using these values. BoxWeight no longer initialize these values itself. It only needs to initialize the value unique to it i.e. weight. This leaves Box free to make these values private if desired.
In the preceding example, super() was called with three arguments. Since constructors can be overloaded, super() can use any form defined by the super class. The constructor executed will be one that matches the arguments.
The line number 18 in the BoxWeightDemo1.java class will result in run-time exception as we are trying to down-cast a super class reference which is actually pointing to a super-class object and hence is not a valid down-cast.
Example: The following example illustrates how super can be used to invoke overloaded constructors of the super class.
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);
9 weight = m;
10 }
11 BoxWeight()
12 { super(); weight = -1;
13 }
14 BoxWeight(double len, double m)
15 { super(len); weight = m;
16 }
17 }
1 class BoxWeightDemo2
2 { public static void main(String args[])
3 { BoxWeight bw1 = new BoxWeight(10,20,15,34.3);
4 BoxWeight bw2 = new BoxWeight(2,3,4,14.2);
5 BoxWeight bw3 = new BoxWeight();
6 BoxWeight bw4 = new BoxWeight(3,5);
7 BoxWeight bw5 = new BoxWeight(bw1);
8 double vol;
9 vol = bw1.volume();
10 System.out.println(vol);
11 System.out.println(bw1.weight);
12 vol = bw2.volume();
13 System.out.println(vol);
14 System.out.println(bw2.weight);
15 vol = bw3.volume();
16 System.out.println(vol);
17 System.out.println(bw3.weight);
18 vol = bw4.volume();
19 System.out.println(vol);
20 System.out.println(bw4.weight);
21 vol = bw5.volume();
22 System.out.println(vol);
23 System.out.println(bw5.weight);
24 }
25 }
Output:
3000.0
34.3
24.0
14.2
-1.0
-1.0
27.0
5.0
3000.0
34.3
Notice that Box(Box ob) is called using super() by passing a parameter of type BoxWeight, not of Box.
As mentioned earlier, super class variable can be used to reference any object derived from that class. Thus, we are able to pass a BoxWeight object to the Box constructor. Of course, Box only has knowledge of its own members.
When a subclass calls super(), it is calling the constructor of its immediate super class. Thus, super() always refers to the super class immediately above the calling class. This is true even in a multi-level hierarchy.
Also, super() must always be the first statement executed inside a sub-class constructor. If a sub-class constructor does not call the super-class’s constructor explicitly then the compiler implicitly puts the call to super-class’s no-argument constructor.
2. Using super to access hidden member of the super class.
The keyword super can be used to access the hidden members of the super as follows:
super.member;
Here, member can be either a method or a data member.
Example: The following example illustrates how to access hidden members of the super- class from sub-class
1 class A
2 { protected int x = 10;
3 public void m1()
4 { System.out.println("m1() of A");
5 }
6 }
1 class B extends A
2 { int x = 5;
3 public void m1()
4 { System.out.println("m1() of B");
5 }
6 public void m2()
7 { System.out.println(super.x);
8 System.out.println(x);
9 m1();
10 super.m1();
11 }
12 public static void main(String args[])
13 { B b = new B(); b.m2();
14 }
15 }
Output:
10
5
m1() of B
m1() of A
Example: The following example illustrates use of super in a multi-level hierarchy.
1 class A
2 { int x = 1;
3 }
1 class B extends A
2 { int y = 2;
3 }
1 class C extends B
2 { int x = 3;
3 void show()
4 { System.out.println(x);
5 System.out.println(y);
6 System.out.println(super.x);
7 }
8 }
1 class Super
2 { static public void main(String args[])
3 { C c = new C();
4 c.show();
5 }
6 }
Output:
3
2
1
Example: The following example also illustrates use of super in a multi-level hierarchy where
all the classes in the hierarchy have the members with the same name.
1 class A
2 { int x = 1;
3 }
1 class B extends A
2 { int y = 2;
3 int x = 4;
4 int getSuperX()
5 {
6 return super.x;
7 }
8 }
1 class C extends B
2 { int x = 3;
3 void show()
4 { System.out.println(x);
5 System.out.println(y);
6 System.out.println(getSuperX());
7 System.out.println(super.x);
8 }
9 }
1 class Super2
2 { static public void main(String args[])
3 { C c = new C();
4 c.show();
5 }
6 }
Output:
3
2
1
4
Since encapsulation is a primary attribute of OOP, it is not surprising that Java provides a as solution to this problem.
Whenever a subclass needs to refer to its immediate super class, it can do so by use of keyword super.
The super has two general uses:
- Calling super class’s constructor
- Accessing a member of the super-class that has been hidden by a member of a sub-class.
1. Using super to call super class constructors
A subclass can call a constructor method defined by its super class by use of the following form of super:
super(parameter-list);
Here, parameter-list specifies any parameters needed by the constructor in the super class. In fact super() must always be the first statement executed inside a subclass’s constructor.
To see how super() is used, consider the improved version of the BoxWeight() class in the following example.
Example:
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(double w, double h, double d, double m)
4 { super(w,h,d);
5 weight = m;
6 }
7 }
1 class BoxWeightDemo1
2 { public static void main(String args[])
3 { BoxWeight bw1 = new BoxWeight(10,20,15,34.3);
4 double vol;
5 vol = bw1.volume();
6 System.out.println(vol);
7 System.out.println(bw1.weight);
8 Box b1 = bw1;
9 vol = b1.volume();
10 System.out.println(vol);
11 //System.out.println(b1.weight); //will not compile
12 //BoxWeight bw2 = b1; //will not compile
13 BoxWeight bw2 = (BoxWeight) b1;
14 vol = bw2.volume();
15 System.out.println(vol);
16 System.out.println(bw2.weight);
17 Box b2 = new Box(5);
18 BoxWeight bw3 = (BoxWeight) b2;
19 System.out.println(bw3.weight);
20
21 }
22 }
Output:
3000.0
34.3
3000.0
3000.0
34.3
Exception in thread "main" java.lang.ClassCastException: Box
at BoxWeightDemo1.main(BoxWeightDemo1.java:18)
Here, BoxWeight() constructor calls super() with the parameters w, h, d. This causes the Box(double w, double h, double d) constructor to be called, which initializes width, height and depth using these values. BoxWeight no longer initialize these values itself. It only needs to initialize the value unique to it i.e. weight. This leaves Box free to make these values private if desired.
In the preceding example, super() was called with three arguments. Since constructors can be overloaded, super() can use any form defined by the super class. The constructor executed will be one that matches the arguments.
The line number 18 in the BoxWeightDemo1.java class will result in run-time exception as we are trying to down-cast a super class reference which is actually pointing to a super-class object and hence is not a valid down-cast.
Example: The following example illustrates how super can be used to invoke overloaded constructors of the super class.
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);
9 weight = m;
10 }
11 BoxWeight()
12 { super(); weight = -1;
13 }
14 BoxWeight(double len, double m)
15 { super(len); weight = m;
16 }
17 }
1 class BoxWeightDemo2
2 { public static void main(String args[])
3 { BoxWeight bw1 = new BoxWeight(10,20,15,34.3);
4 BoxWeight bw2 = new BoxWeight(2,3,4,14.2);
5 BoxWeight bw3 = new BoxWeight();
6 BoxWeight bw4 = new BoxWeight(3,5);
7 BoxWeight bw5 = new BoxWeight(bw1);
8 double vol;
9 vol = bw1.volume();
10 System.out.println(vol);
11 System.out.println(bw1.weight);
12 vol = bw2.volume();
13 System.out.println(vol);
14 System.out.println(bw2.weight);
15 vol = bw3.volume();
16 System.out.println(vol);
17 System.out.println(bw3.weight);
18 vol = bw4.volume();
19 System.out.println(vol);
20 System.out.println(bw4.weight);
21 vol = bw5.volume();
22 System.out.println(vol);
23 System.out.println(bw5.weight);
24 }
25 }
Output:
3000.0
34.3
24.0
14.2
-1.0
-1.0
27.0
5.0
3000.0
34.3
Notice that Box(Box ob) is called using super() by passing a parameter of type BoxWeight, not of Box.
As mentioned earlier, super class variable can be used to reference any object derived from that class. Thus, we are able to pass a BoxWeight object to the Box constructor. Of course, Box only has knowledge of its own members.
When a subclass calls super(), it is calling the constructor of its immediate super class. Thus, super() always refers to the super class immediately above the calling class. This is true even in a multi-level hierarchy.
Also, super() must always be the first statement executed inside a sub-class constructor. If a sub-class constructor does not call the super-class’s constructor explicitly then the compiler implicitly puts the call to super-class’s no-argument constructor.
2. Using super to access hidden member of the super class.
The keyword super can be used to access the hidden members of the super as follows:
super.member;
Here, member can be either a method or a data member.
Example: The following example illustrates how to access hidden members of the super- class from sub-class
1 class A
2 { protected int x = 10;
3 public void m1()
4 { System.out.println("m1() of A");
5 }
6 }
1 class B extends A
2 { int x = 5;
3 public void m1()
4 { System.out.println("m1() of B");
5 }
6 public void m2()
7 { System.out.println(super.x);
8 System.out.println(x);
9 m1();
10 super.m1();
11 }
12 public static void main(String args[])
13 { B b = new B(); b.m2();
14 }
15 }
Output:
10
5
m1() of B
m1() of A
Example: The following example illustrates use of super in a multi-level hierarchy.
1 class A
2 { int x = 1;
3 }
1 class B extends A
2 { int y = 2;
3 }
1 class C extends B
2 { int x = 3;
3 void show()
4 { System.out.println(x);
5 System.out.println(y);
6 System.out.println(super.x);
7 }
8 }
1 class Super
2 { static public void main(String args[])
3 { C c = new C();
4 c.show();
5 }
6 }
Output:
3
2
1
Example: The following example also illustrates use of super in a multi-level hierarchy where
all the classes in the hierarchy have the members with the same name.
1 class A
2 { int x = 1;
3 }
1 class B extends A
2 { int y = 2;
3 int x = 4;
4 int getSuperX()
5 {
6 return super.x;
7 }
8 }
1 class C extends B
2 { int x = 3;
3 void show()
4 { System.out.println(x);
5 System.out.println(y);
6 System.out.println(getSuperX());
7 System.out.println(super.x);
8 }
9 }
1 class Super2
2 { static public void main(String args[])
3 { C c = new C();
4 c.show();
5 }
6 }
Output:
3
2
1
4
No comments:
Post a Comment