3. The Car class (subclass) inherits the attributes and methods from the Vehicle class (superclass). And every car has model. Please also add toString method to the Car class to print out the brand, and model of the car.
Class Vehicle {
protected String brand; // Vehicle attribute
Vehicle() // Vehicle constructor
{
Brand = “Ford”;
}
public void honk() { // Vehicle method
System.out.println("Tuut, tuut!");
}
}
Please help write the Car class. (10 marks)
package vehicle;
public class Vehicle {
protected String Brand; // Vehicle attribute
Vehicle() // Vehicle constructor
{
Brand = "Ford";
}
public void honk() { // Vehicle method
System.out.println("Tuut, tuut!");
}
public static void main(String[] args) {
Vehicle v = new Vehicle();
v.Brand = "Toyota";
Car c= new Car();
c.model = "World Class";
c.display();
}
}
class Car extends Vehicle{
protected String model;
public String toString(){
return "Brand: "+ Brand +"\nModel is: "+model;
}
public void display(){
honk();
System.out.println( toString());
}
}
Comments
Leave a comment