-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCh11_61_Practice19.java
More file actions
175 lines (140 loc) · 5.5 KB
/
Ch11_61_Practice19.java
File metadata and controls
175 lines (140 loc) · 5.5 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
public class Ch11_61_Practice19 {
interface Connectable {
void connectToWifi(String wifiName);
void disconnectToWifi();
default void checkConnectionStatus(){
System.out.println("Checking network status...");
}
}
interface Chargeable{
void startCharging();
void stopCharging();
default int getBatteryLevel(){
return 100;
}
}
interface SmartDevice extends Connectable {
void runDiagnostic();
}
abstract static class Device{
private String brand;
private String model;
Device(String brand , String model){
this.brand = brand;
this.model = model;
}
abstract void powerOn();
abstract void performFunction(String func);
public void displayInfo(){
System.out.println("Device com: " + this.brand);
System.out.println("Device model: " + this.model);
}
}
static class Smartphone079 extends Device implements SmartDevice , Chargeable {
int storageGB;
Smartphone079(String brand , String model , int storage){
super(brand , model);
this.storageGB = storage;
}
public void runDiagnostic(){
System.out.println("Running Diagnostics...");
}
public void displayInfo(){
super.displayInfo();
System.out.println("Storage in GB: " + this.storageGB);
}
@Override
public void powerOn(){
System.out.println("Device powering on...");
}
@Override
public void performFunction(String func){
System.out.println("Performing: " + func);
}
public void connectToWifi(String wifiName){
System.out.println("Connecting to " + wifiName);
}
public void disconnectToWifi(){
System.out.println("Dis-connecting to WIFI...");
}
public void startCharging(){
System.out.println("Charging started...");
}
public void stopCharging(){
System.out.println("Charging stopped...");
}
@Override
public int getBatteryLevel(){
return 85;
}
}
static class Laptop extends Device implements Connectable , Chargeable {
double screenSizeInches;
Laptop(String brand , String model , double size){
super(brand , model);
this.screenSizeInches = size;
}
public void displayInfo(){
super.displayInfo();
System.out.println("Screen size: " + this.screenSizeInches);
}
@Override
public void powerOn(){
System.out.println("Laptop powering on...");
}
@Override
public void performFunction(String func){
System.out.println("Performing: " + func);
}
public void connectToWifi(String wifiName){
System.out.println("Connecting to " + wifiName);
}
public void disconnectToWifi(){
System.out.println("Dis-connecting to WIFI...");
}
public void startCharging(){
System.out.println("Charging started...");
}
public void stopCharging(){
System.out.println("Charging stopped...");
}
@Override
public void checkConnectionStatus(){
System.out.println("Connection status: GOOD");
}
}
public static void main(String[] args) {
// In this practice code -
// Part1 - Abstract class and Basic Inheritance
// Part2 - Interfaces and Multiple Inheritance
// Part3 - Polymorphism with Interface and Abstract classes
Smartphone079 onePlus = new Smartphone079("OnePlus" , "7T" , 512);
Smartphone079 motorola = new Smartphone079("Motorola" , "Edge 60 Stylus" , 256);
Laptop asus = new Laptop("Asus" , "X541UJ" , 14.0);
Laptop lenovo = new Laptop("Lenovo" , "LOQ 15IRX9" , 15.6);
// Accessing Device class (Polymorphism)
System.out.println("** Class - Device:");
Device [] devs = {onePlus , motorola , asus , lenovo};
for (Device element1 : devs){
element1.powerOn();
element1.displayInfo();
element1.performFunction("Flight Mode"); System.out.println();
}
System.out.println();
// Accessing Connectabale interface (Polymorphism)
System.out.println("** Interface - Connectables:");
Connectable [] wifiDevices = {onePlus , lenovo};
for (Connectable element2 : wifiDevices){
element2.connectToWifi("MyHome_5Ghz");
element2.checkConnectionStatus();
element2.disconnectToWifi(); System.out.println();
}
System.out.println();
// Calling specific method in Smartphone
Chargeable battery = new Smartphone079("Asus" , "ROG Phone 5" , 1024);// Bound to Chargeable Interface no other methods allowed.
System.out.println("Battery Level: " + battery.getBatteryLevel()); System.out.println(); System.out.println();
// Inheritance in Interfaces
motorola.runDiagnostic(); System.out.println();
System.out.println("*** End ***");
}
}