A reference to a sub-class object can be stored into a reference type variable of base-class/super-class without any explicit conversion, this is called up-casting. This is like widening conversion used in case of primitive types and takes place implicitly.
A reference to a base-class/super-class object can not be stored into a reference type variable of sub-class without explicit conversion, this is called down-casting. This is like narrowing conversion used in case of primitive types and needs explicit type-casting. The down-casting is valid only if the super class reference points to the sub-class object before down-casting.
Super class variable can refer to a sub-class object
Example: The following example illustrates that a super-class variable can refer to a sub-class object.
Reference variable of a super class can be assigned a reference to any subclass derived from that super class. It is important to understand that it is the type of the reference variable not the type of the object that it refers to that determine what can be accessed.
That is, when a reference to a subclass object is assigned to a super class reference variable, you will have access only to those parts of the object defined by the super class. This is why Box reference cannot access weight even when it refers to a BoxWeight object. If you think about it, this makes sense, because the super class has no knowledge of what a subclass adds to it. This is why the last line of following code is commented out. It is not possible for a Box reference to access the weight field, because it does not define one.
1 class Box
2 { double width, height, depth;
3 double volume()
4 { double vol = width * height * depth;
5 return vol;
6 }
7 Box(double w, double h, double d)
8 { width = w;
9 height = h;
10 depth = d;
11 }
12 Box()
13 { width = -1;
14 height = -1;
15 depth = -1;
16 }
17 Box(double len)
18 { width = height = depth = len;
19 }
20 Box(Box ob)
21 { width = ob.width;
22 height = ob.height;
23 depth = ob.depth;
24 }
25 }
1 class BoxWeight extends Box
2 {
3 double weight;
4 BoxWeight(double w, double h, double d, double m)
5 { width = w;
6 height = h;
7 depth = d;
8 weight = m;
9 }
10 }
1 class BoxWeightDemo
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,51);
5 double vol;
6 vol = bw1.volume();
7 System.out.println(vol);
8 System.out.println(bw1.weight);
9 vol = bw2.volume();
10 System.out.println(vol);
11 System.out.println(bw2.weight);
12 Box b1 = new Box();
13 vol = b1.volume();
14 System.out.println(vol);
15 b1 = bw1;
16 vol = b1.volume();
17 System.out.println(vol);
18 //System.out.println(b1.weight); will not compile
19 }
20 }
Output:
3000.0
34.3
24.0
51.0
-1.0
3000.0
Here BoxWeight inherits all of the characteristics of Box and adds the weight component. A major advantage of inheritance is that once you have created a super class that defines the
attributes common to a set of objects, it can be used to create any number of more specific
subclasses. Each subclass can precisely tailor its own classification.
Remember once you have created a super class that defines the general aspects of an object, that super class can be inherited to form specialized classes. Each subclass simply adds its own, unique attributes.
A reference to a base-class/super-class object can not be stored into a reference type variable of sub-class without explicit conversion, this is called down-casting. This is like narrowing conversion used in case of primitive types and needs explicit type-casting. The down-casting is valid only if the super class reference points to the sub-class object before down-casting.
Super class variable can refer to a sub-class object
Example: The following example illustrates that a super-class variable can refer to a sub-class object.
Reference variable of a super class can be assigned a reference to any subclass derived from that super class. It is important to understand that it is the type of the reference variable not the type of the object that it refers to that determine what can be accessed.
That is, when a reference to a subclass object is assigned to a super class reference variable, you will have access only to those parts of the object defined by the super class. This is why Box reference cannot access weight even when it refers to a BoxWeight object. If you think about it, this makes sense, because the super class has no knowledge of what a subclass adds to it. This is why the last line of following code is commented out. It is not possible for a Box reference to access the weight field, because it does not define one.
1 class Box
2 { double width, height, depth;
3 double volume()
4 { double vol = width * height * depth;
5 return vol;
6 }
7 Box(double w, double h, double d)
8 { width = w;
9 height = h;
10 depth = d;
11 }
12 Box()
13 { width = -1;
14 height = -1;
15 depth = -1;
16 }
17 Box(double len)
18 { width = height = depth = len;
19 }
20 Box(Box ob)
21 { width = ob.width;
22 height = ob.height;
23 depth = ob.depth;
24 }
25 }
1 class BoxWeight extends Box
2 {
3 double weight;
4 BoxWeight(double w, double h, double d, double m)
5 { width = w;
6 height = h;
7 depth = d;
8 weight = m;
9 }
10 }
1 class BoxWeightDemo
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,51);
5 double vol;
6 vol = bw1.volume();
7 System.out.println(vol);
8 System.out.println(bw1.weight);
9 vol = bw2.volume();
10 System.out.println(vol);
11 System.out.println(bw2.weight);
12 Box b1 = new Box();
13 vol = b1.volume();
14 System.out.println(vol);
15 b1 = bw1;
16 vol = b1.volume();
17 System.out.println(vol);
18 //System.out.println(b1.weight); will not compile
19 }
20 }
Output:
3000.0
34.3
24.0
51.0
-1.0
3000.0
Here BoxWeight inherits all of the characteristics of Box and adds the weight component. A major advantage of inheritance is that once you have created a super class that defines the
attributes common to a set of objects, it can be used to create any number of more specific
subclasses. Each subclass can precisely tailor its own classification.
Remember once you have created a super class that defines the general aspects of an object, that super class can be inherited to form specialized classes. Each subclass simply adds its own, unique attributes.
No comments:
Post a Comment