Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

22) Consider the following class hierarchy: public class Vehicle { private Strin

ID: 3657016 • Letter: 2

Question

22) Consider the following class hierarchy: public class Vehicle { private String type; public Vehicle(String type) { this.type = type; } public String displayInfo() { return type; } } public class LandVehicle extends Vehicle { public LandVehicle(String type) { super(type); } } public class Auto extends LandVehicle { public Auto(String type) { super(type); } } You have written a program to use these classes, as shown in the following code snippet: public class VehicleTester { public static void main(String[] args) { Auto myAuto = new Auto("sedan"); System.out.println("MyAuto type = " + ______); } } Complete the code in this program snippet to correctly display the auto's type. a) myAuto.displayInfo() b) myAuto.super.displayInfo() c) myAuto.super.super.displayInfo() d) This cannot be done unless the Auto class overrides the displayInfo method.

Explanation / Answer

option (A) is correct. (myAuto.displayInfo() ) here superclass method displayinfo() is declared as public. So all the subclasses can access the method directly. Simce Auto is subclass of Vehicle (because LandVehicle is subclass of Vehicle and Auto is subclass of LanVehicle), Auto variable can directly access the method.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote