Post

Created by @nathanedwards
 at November 1st 2023, 8:04:00 am.

Exam Question:

// Consider the following code snippet:

class Vehicle {
    protected String brand;
    protected int year;
    
    public Vehicle(String brand, int year) {
        this.brand = brand;
        this.year = year;
    }
    
    public String getBrand() {
        return brand;
    }
    
    public int getYear() {
        return year;
    }
    
    public String toString() {
        return "Brand: " + brand + ", Year: " + year;
    }
}

class Car extends Vehicle {
    private int numOfDoors;
    
    public Car(String brand, int year, int numOfDoors) {
        super(brand, year);
        this.numOfDoors = numOfDoors;
    }
    
    public int getNumOfDoors() {
        return numOfDoors;
    }
    
    public String toString() {
        return super.toString() + ", Number of Doors: " + numOfDoors;
    }
}

class Motorcycle extends Vehicle {
    private boolean hasSidecar;
    
    public Motorcycle(String brand, int year, boolean hasSidecar) {
        super(brand, year);
        this.hasSidecar = hasSidecar;
    }
    
    public boolean hasSidecar() {
        return hasSidecar;
    }
    
    public String toString() {
        return super.toString() + ", Has Sidecar: " + hasSidecar;
    }
}

public class Main {
    public static void main(String[] args) {
        Vehicle vehicle1 = new Car("Toyota", 2018, 4);
        Vehicle vehicle2 = new Motorcycle("Honda", 2020, false);
        
        System.out.println(vehicle1);
        System.out.println(vehicle2);
        
        // Write a line of code that assigns the vehicle2 object
        // to a new Vehicle reference variable called vehicle3.
        // Use appropriate casting if necessary.
        // Print the brand and year of vehicle3 using its
        // respective getter methods.
    }
}

Answer:

The correct line of code to assign the vehicle2 object to a new Vehicle reference variable called vehicle3, using appropriate casting if necessary, is as follows:

Vehicle vehicle3 = (Vehicle) vehicle2;

To print the brand and year of vehicle3 using its respective getter methods, the following lines of code need to be added after assigning vehicle2 to vehicle3:

System.out.println("Brand: " + vehicle3.getBrand());
System.out.println("Year: " + vehicle3.getYear());

Explanation:

In the given code snippet, there is a superclass Vehicle and two subclasses Car and Motorcycle. The Vehicle class has instance variables brand and year, along with their respective getters. It also has a toString() method which returns the string representation of the vehicle's brand and year. The Car class extends Vehicle and has an additional instance variable numOfDoors along with its getter. It overrides the toString() method to include the number of doors as well. Similarly, the Motorcycle class extends Vehicle and has an additional instance variable hasSidecar along with its getter. It also overrides the toString() method to include the information about the sidecar.

In the Main class, two objects are created - vehicle1 of type Car and vehicle2 of type Motorcycle. The vehicle1 object represents a car made by Toyota in 2018 with 4 doors, and the vehicle2 object represents a motorcycle made by Honda in 2020 without a sidecar.

To assign the vehicle2 object to a new Vehicle reference variable called vehicle3, the proper casting is required because vehicle2 is of type Motorcycle and vehicle3 should be of type Vehicle. Since Motorcycle is a subclass of Vehicle, an upcast can be performed by using (Vehicle) to indicate the type casting. Thus, the line of code to assign vehicle2 to vehicle3 is:

Vehicle vehicle3 = (Vehicle) vehicle2;

After assigning vehicle2 to vehicle3, we can print the brand and year of vehicle3 using its respective getter methods. The code to accomplish that is:

System.out.println("Brand: " + vehicle3.getBrand());
System.out.println("Year: " + vehicle3.getYear());

By running the modified code, the output will display the brand and year of the vehicle represented by vehicle2, which is "Honda" and "2020" respectively.