-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathDemoSuper.java
More file actions
104 lines (81 loc) · 2.43 KB
/
DemoSuper.java
File metadata and controls
104 lines (81 loc) · 2.43 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
101
102
103
104
package chapter8;
class Box3 {
private double width;
private double height;
private double depth;
Box3(Box3 ob) {
width = ob.width;
height = ob.height;
depth = ob.depth;
}
Box3(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
Box3() {
width = -1;
height = -1;
depth = -1;
}
Box3(double len) {
width = height = depth = len;
}
//Compute and return volume
double volume() {
return width * height * depth;
}
}
class BoxWeight3 extends Box3 {
double weight;//weight of box
//Constructor clone of an object
BoxWeight3(BoxWeight3 ob) {// Pass object to constructor..
super(ob);
weight = ob.weight;
}
//Constructor when all parameters are specified
BoxWeight3(double w, double h, double d, double m) {
super(w, h, d);
weight = m;
}
//Default constructor
BoxWeight3() {
super();
weight = -1;
}
//Constructor when cube is created
BoxWeight3(double len, double m) {
super(len);
weight = m;
}
}
public class DemoSuper {
public static void main(String[] args) {
BoxWeight3 myBox1 = new BoxWeight3(10, 20, 15, 34.3);
BoxWeight3 myBox2 = new BoxWeight3(2, 3, 4, 0.076);
BoxWeight3 myBox3 = new BoxWeight3();//default
BoxWeight3 myCube = new BoxWeight3(3, 2);
BoxWeight3 myClone = new BoxWeight3(myBox1);
double vol;
vol=myBox1.volume();
System.out.println("Volume of myBox1 is : "+vol);
System.out.println("Weight of myBox1 is : "+myBox1.weight);
System.out.println();
vol=myBox2.volume();
System.out.println("Volume of myBox2 is : "+vol);
System.out.println("Weight of myBox2 is : "+myBox2.weight);
System.out.println();
vol=myBox3.volume();
System.out.println("Volume of myBox3 is : "+vol);
System.out.println("Weight of myBox3 is : "+myBox3.weight);
System.out.println();
vol=myClone.volume();
System.out.println("Volume of myClone is : "+vol);
System.out.println("Weight of myClone is : "+myClone.weight);
System.out.println();
vol=myCube.volume();
System.out.println("Volume of myCube is : "+vol);
System.out.println("Weight of myCube is : "+myCube.weight);
System.out.println();
}
}