-
Notifications
You must be signed in to change notification settings - Fork 231
Expand file tree
/
Copy pathconstructorOverloading.java
More file actions
48 lines (35 loc) · 955 Bytes
/
constructorOverloading.java
File metadata and controls
48 lines (35 loc) · 955 Bytes
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
class Box {
double width, height, depth;
// constructor used when all dimensions
// specified
Box(double w, double h, double d)
{
width = w;
height = h;
depth = d;
}
// constructor used when no dimensions
// specified
Box() { width = height = depth = 0; }
// constructor used when cube is created
Box(double len) { width = height = depth = len; }
// compute and return volume
double volume() { return width * height * depth; }
}
public class constructorOverloading {
public static void main(String args[])
{
// creating boxes using constructors
Box mybox1 = new Box(10, 20, 15);
Box mybox2 = new Box();
Box mycube = new Box(7);
double vol;
// get volume of first box
vol = mybox1.volume();
System.out.println("Volume of mybox1 is " + vol);
vol = mybox2.volume();
System.out.println("Volume of mybox2 is " + vol);
vol = mycube.volume();
System.out.println("Volume of mycube is " + vol);
}
}