-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMethodOverloading.java
More file actions
46 lines (38 loc) · 1.08 KB
/
Copy pathMethodOverloading.java
File metadata and controls
46 lines (38 loc) · 1.08 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
/* 10. Complete the following program that demonstrate the concept of constructor overloading. */
/*
* author: @pray3m
*/
class Box3 {
double width, height, depth;
// constructor used when all dimensions // specified
Box3(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
Box3() {
}
Box3( double d) {
width = height = depth =d ;
}
// compute and return volume
double volume() {
return width * height * depth;
}
}
public class MethodOverloading {
public static void main(String[] args) {
// create boxes using the various constructors
Box3 mbox1 = new Box3(10, 20, 15);
Box3 mbox2 = new Box3();
Box3 mbox3 = new Box3(7);
double vol;
// get volume of first box
vol = mbox1.volume();
System.out.println(" Volume of with 3 parameters is " + vol);
vol = mbox2.volume();
System.out.println(" Volume of with no parameters is " + vol);
vol = mbox3.volume();
System.out.println(" Volume of cube " + vol);
}
}