-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathSuperEx1.java
More file actions
100 lines (82 loc) · 2.47 KB
/
SuperEx1.java
File metadata and controls
100 lines (82 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
Understanding Super
This program demostrate the need for super
in java. Using super we can initialize the private
members of inherited class.
This also enhances security and make our code
reliable.
*/
package src.learning.understanding_super;
class BuildBox {
private double width;
private double height;
private double depth;
// construtor when all dimensions is given
BuildBox(double width, double height, double depth){
this.width = width;
this.height = height;
this.depth = depth;
}
// constructor when object is cloned
BuildBox(BuildBox obj){
this.width = obj.width;
this.height = obj.height;
this.depth = obj.depth;
}
// constrctor when cube is formed
BuildBox(double height){
this.width = this.height = this.depth = height;
}
// constructor when no mesurements are given
BuildBox(){
this.width = -1;
this.height = -1;
this.depth = -1;
}
void volume(){
System.out.println("Volume: "+(this.width * this.height * this.depth));
}
}
class BoxWeight extends BuildBox {
double weight;
// constructor when all dimensions are mentioned along with weigth
BoxWeight(double weight, double width, double height, double depth){
super(width, height, depth);
this.weight = weight;
}
// constructor when BoxWeight object is cloned
BoxWeight(BoxWeight obj){
super(obj);
this.weight = obj.weight;
}
// constructor when cube is created
BoxWeight(double weight, double height){
super(height);
this.weight = weight;
}
// constructor when no dimension and weight is given
BoxWeight(){
super();
this.weight = -1;
}
}
class DemoSuper {
public static void main(String[] args) {
// object with all three dimensions given along with the weight
// constructor at line 39 is called
BoxWeight b1 = new BoxWeight(100, 30, 20, 10);
b1.volume();
// cloned object
// constructor at line 45 is called
BoxWeight cloneBox = new BoxWeight(b1);
cloneBox.volume();
// cube object
// constructor at line 51 is called
BoxWeight cube = new BoxWeight(100, 20);
cube.volume();
// object when no dimensions or weight are given.
// constructor at line 57 is called
BoxWeight b2 = new BoxWeight();
b2.volume();
}
}