Repository navigation
Expand file tree
/
Copy pathQ2_Inheritance.java
More file actions
68 lines (55 loc) · 1.99 KB
/
Copy pathQ2_Inheritance.java
File metadata and controls
68 lines (55 loc) · 1.99 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
public class Q2_Inheritance {
// ── PREDICT: What does main() print for lines 1-4? ──
static class Motor {
String name() { return "Motor"; }
void run() { System.out.println(name() + " spinning"); }
}
static class Falcon extends Motor {
String name() { return "Falcon"; }
}
static class Kraken extends Falcon {
void run() { System.out.println(name() + " go brrr"); }
}
public static void main(String[] args) {
Motor m1 = new Falcon();
System.out.print("1: "); m1.run();
//1: 1: Falcon spinning
Motor m2 = new Kraken();
System.out.print("2: "); m2.run();
//2: 2: Falcon go brrr
Falcon m3 = new Kraken();
System.out.println("3: " + m3.name());
//3: Falcon
Motor m4 = new Kraken();
System.out.println("4: " + (m4 instanceof Falcon));
//4: true
//Kinda guessed through the answers on this one
// ── WRITE ──
// Create a class "Neo" that extends Motor such that:
// - neo.name() returns "Neo"
// - neo.run() prints "Neo spinning at {rpm} RPM"
// where rpm is set via a constructor argument
// - neo.run() must call super.name() (not hardcode "Neo")
//
// Uncomment the test below when done.
// Neo n = new Neo(6000);
// System.out.print("5: "); n.run(); // should print: Neo spinning at 6000 RPM
// assert n.name().equals("Neo");
// assert n instanceof Motor;
// System.out.println("Neo ok");
}
// TODO: write the Neo class here
static class Neo extends Motor {
static int rpm;
Neo(int rpm) {
this.rpm = rpm;
}
String name() {
return "Neo";
}
void run() {
System.out.println(super.name() + " spinning at " + rpm + " RPM");
}
}
// don't really get why super.name() is used in the run method instead of just name()
}