123k views
1 vote
Using your choice of C# or Java (NOT in Pseudocode), write an abstract class Vehicle. The Vehicle class contains at least these two attributes: id (int type) and model (String type) and an abstract method vehicleDetail() that will have two arguments for the vehicle id and model. Then write two concrete classes Car and Bus that inherit the Vehicle class and implement the abstract method that displays vehicle id and model. You don't need to include the main () method.

User Moebius
by
4.9k points

2 Answers

1 vote

Answer:

//Declare the abstract class

//by using the "abstract" keyword as follows

abstract class Vehicle {

//declare the attributes of the class

int id;

String model;

//declare the abstract method, vehicleDetail()

//which has two arguments: id and model

//and has no implementation

abstract void vehicleDetail(int id, String model);

} //End of abstract class declaration

========================================================

//Create the Car class to inherit from the Vehicle class

//by using the "extends" keyword as follows

public class Car extends Vehicle {

//Implement the abstract method in the Vehicle class

//writing statements to print out the id and model as follows;

void vehicleDetail(int id, String model) {

System.out.println("Id : " + id);

System.out.println("Model : " + model);

} //End of vehicleDetail method

} //End of class declaration

==========================================================

//Create the Bus class to inherit from the Vehicle class

//by using the "extends" keyword as follows

public class Bus extends Vehicle {

//Implement the abstract method in the Vehicle class

//by writing statements to print out the id and model as follows;

void vehicleDetail(int id, String model) {

System.out.println("Id : " + id);

System.out.println("Model : " + model);

} //End of vehicleDetail method

} //End of class declaration

================================================================

Step-by-step explanation:

The above code has been written in Java and it contains comments explaining every line of the code. Please go through the comments.

The actual lines of code have been written in bold-face to differentiate them from comments.

User JuggernautDad
by
5.9k points
3 votes

Answer:

public abstract class Vehicle

{

private int id;

public int ID

{

get { return id; }

set { id = value; }

}

private string model;

public string Model

{

get { return model; }

set { model = value; }

}

public abstract string VehicleDetail(int id, string model);

}

public class Car : Vehicle

{

public Car() { }

public Car(int id, string model)

{

ID = id;

Model = model;

}

public override string VehicleDetail(int id, string model)

{

return $"{id} - {model}";

}

}

public class Bus : Vehicle

{

public Bus(int id, string model, string make)

{

ID = id;

Model = model;

Make = make;

}

public string Make { get; set; }

public override string VehicleDetail(int id, string model, string make)

{

return $"{id} - {model}" - {make};

}

}

Step-by-step explanation:

User Zahid Khan
by
4.4k points