-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2. Constructor Overloading.java
More file actions
43 lines (40 loc) · 1002 Bytes
/
2. Constructor Overloading.java
File metadata and controls
43 lines (40 loc) · 1002 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
//Write a program to define a class , describe its constructor , overlaod the constructor and initiate the object.
import java.util.*;
class Box
{
double width, height, depth;
Box(double w, double h, double d)
{
width = w;
height = h;
depth = d;
}
Box()
{
width = height = depth = 0;
}
Box(double len)
{
width = height = depth = len;
}
double volume()
{
return width * height * depth;
}
}
public class MyJClass2
{
public static void main(String args[])
{
Box box1 = new Box(10, 24, 14);
Box box2 = new Box();
Box box3 = new Box(8);
double vol;
vol = box1.volume();
System.out.println(" Volume of box1 is " + vol);
vol = box2.volume();
System.out.println(" Volume of box2 is " + vol);
vol = box3.volume();
System.out.println(" Volume of box3 is " + vol);
}
}