Knowee
Questions
Features
Study Tools

class Vehicle { protected int wheelsCount; private int doorsCount; public Vehicle(int wheelsCount, int doorsCount) { this.wheelsCount = wheelsCount; this.doorsCount = doorsCount; }}public class Truck extends Vehicle { private double loadCapacity; private int weight; public Truck(int wheelsCount, int doorsCount, double loadCapacity, int weight) { /* Missing Code */ }}

Question

class Vehicle { protected int wheelsCount; private int doorsCount; public Vehicle(int wheelsCount, int doorsCount) { this.wheelsCount = wheelsCount; this.doorsCount = doorsCount; }}public class Truck extends Vehicle { private double loadCapacity; private int weight; public Truck(int wheelsCount, int doorsCount, double loadCapacity, int weight) { /* Missing Code */ }}

🧐 Not the exact question you are looking for?Go ask a question

Solution

In the Truck class constructor, you are extending the Vehicle class, so you need to use the super keyword to call the parent class constructor. Here is the missing code:

public Truck(int wheelsCount, int doorsCount, double loadCapacity, int weight) {
    super(wheelsCount, doorsCount);
    this.loadCapacity = loadCapacity;
    this.weight = weight;
}

The super keyword in Java is a reference variable that is used to refer to the immediate parent class object. In the constructor of the Truck class, we are calling the constructor of the Vehicle class (parent class) using the super keyword, passing the wheelsCount and doorsCount parameters. Then we are initializing the loadCapacity and weight fields of the Truck class.

This problem has been solved

Similar Questions

class FourWheeler{    public FourWheeler()    {        System.out.println("Class FourWheeler");    }}class Car extends FourWheeler{    public Car()     {        System.out.println("Class Car");        }}class Audi extends Car{   public Audi()    {        super();        System.out.println("Class Audi");    }}class Driver{    public static void main(String args[])    {        Audi cc=new Audi();    }}

Observe the below code.  You have a class InsurancePolicy and another class VehicleInsurancePolicy that inherits InsurancePolicy.InsurancePolicy.javapublic class InsurancePolicy { private int policyNumber; private String holderName; private double amount; double calculateBonusAmount(){ //some logic return 0; } }VehicleInsurancePolicy.javaclass VehicleInsurancePolicy extends InsurancePolicy { private String vehicleNumber; private int noOfMonths;   double calculateBonusAmount(){ //some logic return 0; }}Which access specifier can be given to the overridden method calculateBonusAmount.

10. class FourWheeler {11.  blank void display() { }12. }13. class Car extends FourWheeler {14.   blank void display() { }15. }Which method at line 14, will correctly complete class Car?protected  private

Create a class Car. Car has  its data members speed and colour under private. Void speed(int car_speed) and void colour(string car_colour) are the member functions in class Car. Create one more class Audi. Audi wants to access the private data members of Class Car. Along with the inherited method, Audi has 5 airbags and so it has additional member function void air_bag_count( ). There is one more class Audi_a6 which is the latest version and inludes all the features of  audi along with additional ones. It has additional vent for AC ...and so it has additional member function Car_ac( ) .Identify the type of inheritance..Create object for each class, supply value to all classes and display speed and colour for car, speed, colour and airbag details for audi. And car speed, colour, airbag detail, ac details for Audi_A6?

Develop a Java Application with ‘Vehicle’ Class with vehicle type, vehicle number, as member. Inherit the classes bicycle, car, bike, and lorry from vehicle class. Consider car and bike only as passenger vehicle. Get inputs engine number, chassis number, top speed, number of passengers, number of gears for Passenger vehicle, and if is not a passenger vehicle, get inputs total amount of load to carry for the respective inherited classes. Generate a report for the vehicles. Implement the concept using inheritance and method overriding.

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.