Spaces:
Running
Running
| The programme defines the class Boat. | |
| Write three classes that inherit the class Boat: | |
| Sailboat | |
| Rowingboat | |
| Canoe | |
| There is no need to define any properties for the classes, an empty block is sufficient. | |
| import java.util.Random; | |
| public class Test{ | |
| public static void main(String[] args){ | |
| final Random r = new Random(); | |
| System.out.println("Creating objects..."); | |
| Sailboat sb = new Sailboat(); | |
| Rowingboat rb = new Rowingboat(); | |
| Canoe c = new Canoe(); | |
| System.out.println("Objects created!"); | |
| System.out.println("Testing inheritance..."); | |
| System.out.println("Sailboat inherits Boat: " + | |
| (sb instanceof Boat ? "Ok" : "No")); | |
| System.out.println("Rowing boat inherits Boat: " + | |
| (rb instanceof Boat ? "Ok" : "No")); | |
| System.out.println("Canoe inherits Boat: " + | |
| (c instanceof Boat ? "Ok" : "No")); | |
| } | |
| } | |
| class Boat {} | |
| class Sailboat extends Boat {} | |
| class Rowingboat extends Boat {} | |
| class Canoe extends Boat {} | |
| Creating objects... | |
| Objects created! | |
| Testing inheritance... | |
| Sailboat inherits Boat: Ok | |
| Rowing boat inherits Boat: Ok | |
| Canoe inherits Boat: Ok | |