-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP44_ConstructorOverloading.java
More file actions
50 lines (41 loc) · 1.28 KB
/
Copy pathP44_ConstructorOverloading.java
File metadata and controls
50 lines (41 loc) · 1.28 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
package programs;
/**
* ============================================================
* PROGRAM 44: Constructor Overloading and Chaining (this)
* ============================================================
* Problem: WAP to demonstrate Constructor Overloading and
* Constructor Chaining using `this(...)`.
* ============================================================
*/
class Laptop {
String brand;
int ramGb;
double price;
// No-arg constructor
public Laptop() {
this("Generic", 8, 499.99); // chains to 3-arg constructor
}
// 2-arg constructor
public Laptop(String brand, int ramGb) {
this(brand, ramGb, 799.99);
}
// 3-arg constructor
public Laptop(String brand, int ramGb, double price) {
this.brand = brand;
this.ramGb = ramGb;
this.price = price;
}
public void printSpecs() {
System.out.printf(" Laptop Specs: %s | %d GB RAM | $%,.2f%n", brand, ramGb, price);
}
}
public class P44_ConstructorOverloading {
public static void main(String[] args) {
Laptop l1 = new Laptop();
Laptop l2 = new Laptop("Dell XPS", 16);
Laptop l3 = new Laptop("Apple MacBook Pro", 32, 2499.00);
l1.printSpecs();
l2.printSpecs();
l3.printSpecs();
}
}