Suppose you have a Vehicle base class as follows: class Vehicle { public String
ID: 638574 • Letter: S
Question
Suppose you have a Vehicle base class as follows:
class Vehicle {
public String vehicleType;
}
Suppose you further extend the Vehicle class to create a Car class:
public class Car extends Vehicle {
private String carModel;
public void printDetails() {
vehicleType = "Car"; // Set value of public field in Vehicle class
carModel = "Toyota"; // Set value of private field in Car class
System.out.println(carModel +" "+ vehicleType);
}
public static void main(String[] args) {
Car myCar =new Car();
myCar.printDetails();
}
}
Can you now extend the Vehicle class to make a Bicycle class?
Explanation / Answer
public class Car { String make; String model; int numDoors; Car(String make, String model) { this(make, model, 4); } Car(String make, String model, int numDoors) { this.make = make; this.model = model; this.numDoors = numDoors; } void printDetails() { System.out.println("Make = " + make); System.out.println("Model = " + model); System.out.println("Number of doors = " + numDoors); System.out.println(); } public static void main(String[] args) { Car myCar = new Car("Toyota", "Camry"); myCar.printDetails(); Car yourCar = new Car("Mazda", "RX-8", 2); yourCar.printDetails(); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.