5. exercise: Inheritance and Encapsulation
- Review presentation
- Investigate org.activity05 package
- Create class WaterRoad as a subclass of Road
- Select File — New — Class and enter:
- Name: WaterRoad
- Superclass: org.activity04.Road
- Select: Constructors from seperclass, Inherited abstract methods
- Press Finish
- Select Source — Generate Constructors from Superclass..., select both constructors Road() and Road(String,String,int) and press OK.
- Select Source — Override/Implement methods... and select method toString().
- Override .toString() method which returns string in form: WaterRoad From — To, 00km
- Create class Ship as a subclass of Transport
- Allow to store private byte number of sails for Ship:
- Create Ship(String id, byte sails) constructor,
Override move(Road) to return String in form:
ID Ship is sailing on WaterRoad From — To, 00km with x sails
where x is actual number of sails,
return Cannot sail on (Road representation as String) if it is not WaterRoad.
- Create class Vehicle as subclass of Transport
- Allow to store private int number of wheels for vehicle.
- Implement Vehicle(String id, float consumption, int tankSize, int wheels) constructor,
Override method move(Road) for vehicle, which returns String in form:
ID Vehicle is driving on Road From — To, 00km with x wheels
where x is actual number of wheels,
return Cannot drive on (Road representation as String) if it is not Road.
- Implement Amphibia class in a such way, that it is a Transport:
- Make all internal properties of Amphibia private.
- Implement constructor Amphibia(String id, float consumption, int tankSize, byte sails, int wheels)
- Override move(Road road) method, that Amhibia behaves like a Vehicle on ground road and like a Ship on water road.
- Use .getClass().getSimpleName() to get Type and reuse toString() of Road
- Use super(); to refer to parent class implementation of method or constructor.
- Use if(road instanceof WaterRoad) to check if object is type or sybtype of WaterRoad
- Use if(road.getClass() == Road.class) to check if object is exact type of Road class.