-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOverriding.java
205 lines (168 loc) · 5.21 KB
/
Overriding.java
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Overloading and Overriding
import java.util.Scanner;
// parent
abstract class Car {
String color;
int seats;
Car(String color, int seats) {
this.color = color;
this.seats = seats;
}
abstract void show();
abstract void get(String color, int seats);
}
// child
class Honda extends Car {
Honda(String color, int seats) {
super(color, seats);
}
@Override
void show() {
System.out.println("Color of Honda car: " + color);
System.out.println("Seats of Honda car: " + seats);
}
@Override
void get(String color, int seats) {
this.color = color;
this.seats = seats;
System.out.println("Success");
}
void price(int tube, int speaker) {
System.out.println("Total price " + (tube + speaker));
}
void price(int tube, int speaker, int lights) {
System.out.println("Total price " + (tube + speaker + lights));
}
}
// child
class Hyundai extends Car {
Hyundai(String color, int seats) {
super(color, seats);
}
@Override
void show() {
System.out.println("Color of Hyundai car: " + color);
System.out.println("Seats of Hyundai car: " + seats);
}
@Override
void get(String color, int seats) {
this.color = color;
this.seats = seats;
System.out.println("Success");
}
}
// child
class Toyota extends Car {
Toyota(String color, int seats) {
super(color, seats);
}
@Override
void show() {
System.out.println("Color of Toyota car: " + color);
System.out.println("Seats of Toyota car: " + seats);
}
@Override
void get(String color, int seats) {
this.color = color;
this.seats = seats;
System.out.println("Success");
}
}
// executing main class
public class Overriding {
public static void main(String[] args) {
System.out.println("Method OverLoading");
for (int i = 0; i < 60; i++) {
System.out.print("*");
}
System.out.println();
Honda ho = new Honda("red", 4);
ho.show();
for (int i = 0; i < 60; i++) {
System.out.print("*");
}
System.out.println();
Hyundai hy = new Hyundai("black", 6);
hy.show();
for (int i = 0; i < 60; i++) {
System.out.print("*");
}
System.out.println();
Toyota to = new Toyota("black", 6);
to.show();
for (int i = 0; i < 60; i++) {
System.out.print("*");
}
System.out.println();
Scanner console = new Scanner(System.in);
System.out.println("Input New Color for Honda & Press Enter");
String color1 = console.next();
System.out.println("Input New Seats for Honda & Press Enter");
int seats1 = console.nextInt();
ho.get(color1, seats1);
for (int i = 0; i < 60; i++) {
System.out.print("*");
}
System.out.println();
System.out.println("Input New Color for Hyundai & Press Enter");
String color2 = console.next();
System.out.println("Input New Seats for Hyundai & Press Enter");
int seats2 = console.nextInt();
hy.get(color2, seats2);
for (int i = 0; i < 60; i++) {
System.out.print("*");
}
System.out.println();
System.out.println("Input New Color for Toyota Press Enter");
String color3 = console.next();
System.out.println("Input New Seats for Toyota & Press Enter");
int seats3 = console.nextInt();
to.get(color3, seats3);
for (int i = 0; i < 60; i++) {
System.out.print("*");
}
System.out.println();
System.out.println("New Settings for all Cars");
ho.show();
hy.show();
to.show();
for (int i = 0; i < 60; i++) {
System.out.print("*");
}
System.out.println();
// 2nd part
System.out.println(" Method Overriding");
for (int i = 0; i < 60; i++) {
System.out.print("*");
}
System.out.println();
System.out.println("Are you Purchasing Tube, Speaker, Lights Press 1 for Honda");
System.out.println("Are you Purchasing Tube, Speaker only Press 2 for Honda");
int choice = console.nextInt();
while (true) {
if (choice == 1) {
System.out.print("Price of tube ");
int t = console.nextInt();
System.out.print("Price of speaker ");
int s = console.nextInt();
System.out.print("Price of light ");
int l = console.nextInt();
ho.price(t, s, l);
break;
} else if (choice == 2) {
System.out.print("Price of tube ");
int t = console.nextInt();
System.out.print("Price of speaker ");
int s = console.nextInt();
ho.price(t, s);
break;
} else {
System.out.println("You entered Wrong Choice");
}
}
for (int i = 0; i < 60; i++) {
System.out.print("*");
}
System.out.println();
}
}