Please help me with these questions, thanks guys! Classes In the following all c
ID: 3726981 • Letter: P
Question
Please help me with these questions, thanks guys!
Classes In the following all classes, interfaces and members are public. Many details have been omitted. All relevant details have been included class Location f...) interface RoadLegal int topSpeed ); bool licenseRequired (); interface PublicTransport String getLicenseType ); float calculateFare (Location , Location 12); class Vehicle [ int travelTime(Location 11, Location 12) int topSpeed ); class GravityPowered extends Vehicle t int getWeight ); int topSpeed ); class Motorised extends Vehicle implements RoadLegal I int travelTime (Location 1, Location 12) int topSpeed ); class Skateboard extends GravityPowered class Bicycle extends GravityPowered implements RoadLegal class MotorBike extends Motorised [ int topSpeed ); int travelTime(Location 11) class Car extends Motorised int topSpeed ); class Taxi extends Car implements PublicTransport () class Bus extends Motorised implements PublicTransport [ int topSpeed );Explanation / Answer
Hello. Let’s analyze each questions one by one. As the question number starts from 2, I’m using the same numbers to represent each questions.
2. For the purpose of this part , assume that xyz is an instance of class XYZ, for each cast, indicate whether it
>Would compile
>Could be implicit
>Could generate a runtime error
a) (Vehicle) bus -> This could be implicit and would compile and run without any errors as the bus is an instance of Bus which extends Motorised which extends Vehicle. Therefore, bus is an instance of Vehicle
b) (Taxi)car -> This will compile but generate a runtime error as the Car cannot be cast to Taxi. We can only cast child classes to super class, but not super class to child class.
c) (Bicycle) motorised -> This will not even compile as there is no relation between Bicycle and Motorised except that both are implementing RoadLegal.
d) (RoadLegal) MotorBike -> This will compile and run with no errors, as the MotorBike implicitly implements RoadLegal interface. There is no need for the casting also (could be implicit).
e) (Vehicle) taxi -> This will compile and run with no errors and could be implicit as the Taxi extends Car which extends Motorised which extends Vehicle. Or in other words taxi is an instance of Vehicle (like grand child)
3. If we wanted a class to represent a Bicycle with a backup motor, where would that fit in the hierarchy?
That’s a tricky question. Currently we can consider any vehicle with motor only under Motorised category. That is, a Bicycle with a backup motor is like a Hybrid one, so it should be under GravityPowered as well as Motorised. As java doesn’t support multiple inheritance, we’ll have to modify the hierarchy to either introduce an interface for Hybrid vehicles or convert Motorised & GravityPowered classes to interfaces so that the BicycleWithMotor can implement them both. If you don’t want to make any modifications, we’ll have to put it under Vehicle class, which will not be very efficient.
4. For each of the following, write where the method definition that actually runs is
a) Vehicle v=new Taxi(); v.topSpeed();
Here, a Taxi object is created, so it will first check if there is any overridden definition for topSpeed() method. As the Taxi class doesn’t have it, it will check in the direct super class of Taxi- which is Car. There is a topSpeed() method definition in Car class, so it will execute.
b) RoadLegal r=new Bicycle(); r.topSpeed();
Here, a Bicycle instance is created successfully as the Bicycle implements RoadLegal interface. But the Bicycle class doesn’t implement the method topSpeed(), but the super class GravityPowered does. So , topSpeed() in GravityPowered class will get executed.
c) Vehicle v= new MotorBike(); v.travelTime(loc1,loc2);
Here, a MotorBike instance is created, so it will first check for any overridden definitions for travelTime(Loc1, Loc2). The MotorBike class has a travelTime() method, but it accept only one Loc argument , so it will not get executed, instead, travelTime(Loc1,Loc2) of the super class Motorised will get executed.
5. In order to properly implement the interfaces they claim to, some classes would need to add some methods, For each class below, give the names of the missing methods.
a) MotorBike
the licenseRequired() method of RoadLegal interface is required
b) Taxi
the getLicenseType() method of PublicTransport interface is required
the calculateFare(Location l1, Location l2) method of PublicTransport interface is required
If you have any doubts, feel free to drop a comment. Thanks