Answer to Question #276095 in Java | JSP | JSF for Candy

Question #276095

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)


1
Expert's answer
2021-12-06T05:26:49-0500
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());
       
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog