KaiquanMah commited on
Commit
5209f8c
·
verified ·
1 Parent(s): 0376331

Create 02B. Boat's inheritors

Browse files
Week 5: Class hierarchies/02B. Boat's inheritors ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ The programme defines the class Boat.
2
+
3
+ Write three classes that inherit the class Boat:
4
+ Sailboat
5
+ Rowingboat
6
+ Canoe
7
+ There is no need to define any properties for the classes, an empty block is sufficient.
8
+
9
+
10
+
11
+ import java.util.Random;
12
+
13
+ public class Test{
14
+ public static void main(String[] args){
15
+ final Random r = new Random();
16
+
17
+
18
+ System.out.println("Creating objects...");
19
+ Sailboat sb = new Sailboat();
20
+ Rowingboat rb = new Rowingboat();
21
+ Canoe c = new Canoe();
22
+
23
+ System.out.println("Objects created!");
24
+ System.out.println("Testing inheritance...");
25
+
26
+ System.out.println("Sailboat inherits Boat: " +
27
+ (sb instanceof Boat ? "Ok" : "No"));
28
+
29
+ System.out.println("Rowing boat inherits Boat: " +
30
+ (rb instanceof Boat ? "Ok" : "No"));
31
+
32
+ System.out.println("Canoe inherits Boat: " +
33
+ (c instanceof Boat ? "Ok" : "No"));
34
+ }
35
+ }
36
+
37
+ class Boat {}
38
+
39
+
40
+ class Sailboat extends Boat {}
41
+ class Rowingboat extends Boat {}
42
+ class Canoe extends Boat {}
43
+
44
+
45
+
46
+
47
+
48
+
49
+ Creating objects...
50
+ Objects created!
51
+ Testing inheritance...
52
+ Sailboat inherits Boat: Ok
53
+ Rowing boat inherits Boat: Ok
54
+ Canoe inherits Boat: Ok
55
+
56
+
57
+
58
+