Overriding toString() in Java classes

Report a typo
Hey there! This problem might be a bit unpredictable, but give it a go and let us know how you do!
Consider the code snippet below where we have a base class 'Vehicle' and a derived class 'Car'. How should you properly override the toString() method in the 'Car' class to add the 'numberOfDoors' info to the original output from the Vehicle's 'toString' method?
public class Vehicle {
  private String color;
  public Vehicle(String color) { this.color = color; }
  public String toString() { return "Vehicle color: " + color; }
}

public class Car extends Vehicle {
  private int numberOfDoors;
  public Car(String color, int numberOfDoors) {
    super(color);
    this.numberOfDoors = numberOfDoors;
  }
  // ... Add toString method here
}
Select one option from the list
___

Create a free account to access the full topic