-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP45_SingleAndMultilevelInheritance.java
More file actions
52 lines (42 loc) · 1.36 KB
/
Copy pathP45_SingleAndMultilevelInheritance.java
File metadata and controls
52 lines (42 loc) · 1.36 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
package programs;
/**
* ============================================================
* PROGRAM 45: Single and Multilevel Inheritance with 'super'
* ============================================================
* Problem: WAP to demonstrate Single and Multilevel Inheritance
* with constructor propagation using `super(...)`.
* ============================================================
*/
// Level 1: Base Class
class DeviceBase {
protected String brand;
public DeviceBase(String brand) {
this.brand = brand;
}
}
// Level 2: Single Inheritance
class Computer extends DeviceBase {
protected int ramGb;
public Computer(String brand, int ramGb) {
super(brand);
this.ramGb = ramGb;
}
}
// Level 3: Multilevel Inheritance
class SmartLaptop extends Computer {
private boolean touchScreen;
public SmartLaptop(String brand, int ramGb, boolean touchScreen) {
super(brand, ramGb);
this.touchScreen = touchScreen;
}
public void showDetails() {
System.out.printf(" SmartLaptop[Brand: %s | RAM: %d GB | TouchScreen: %s]%n",
brand, ramGb, touchScreen ? "Yes" : "No");
}
}
public class P45_SingleAndMultilevelInheritance {
public static void main(String[] args) {
SmartLaptop myLaptop = new SmartLaptop("Apple MacBook", 36, false);
myLaptop.showDetails();
}
}