0

I'm creating a new class which is vehicle. I'm expecting the only protected variables inherit to subclasses. But when I try to create Constructor with IDE, it is also using superclasses private variables? -Which is private String vehicleName- I'm not clearly understanding this situation. Shouldn't I use auto Concstructor?

public class Vehicle {
    protected int capacityOfPassengers;
    protected String mapOfRoute;
    private String vehicleName;

    public Vehicle(int capacityOfPassengers, String mapOfRoute, 
                   String vehicleName) {

        this.capacityOfPassengers = capacityOfPassengers;
        this.mapOfRoute = mapOfRoute;
        this.vehicleName = vehicleName;
    }
}

public class LandVehicle extends Vehicle {
    private String brand;
    private int priceModel;

    public LandVehicle(int capacityOfPassengers, String mapOfRoute, 
                       String vehicleName, String brand, int priceModel) {

        super(capacityOfPassengers, mapOfRoute, vehicleName);
        this.brand = brand;
        this.priceModel = priceModel;
    }
}
ernest_k
  • 44,416
  • 5
  • 53
  • 99
  • Your IDE knows that `LandVehicle` cannot be constructed until a constructor from `Vehicle` is called. Now, `Vehicle` does not have a no-arg constructor, and that means that `LandVehicle` must call the non-default constructor in `Vehicle`. This can only work if it creates a constructor in `LandVehicle` with all parameters of the superclass's constructor, which it uses to call `super(...)` – ernest_k Nov 25 '21 at 20:01
  • 1
    _it is also using superclasses private variables_ Where is it using the superclass' fields? – Sotirios Delimanolis Nov 25 '21 at 20:38

2 Answers2

1

You cannot access vehicleName from LandVehicle. You just pass some string parameter to super constructor, and the super constructor sets the vehicleName. For example, you can't initialize this field inside LandVehicle class as this.vehicleName = vehicleName.

Alex Sveshnikov
  • 4,214
  • 1
  • 10
  • 26
  • I have to give a vehicleName when I try to create new obj like LandVehicle landV = new LandVehicle(25,"Bergen-Cologne", "vehicleName", "Man", 2009); But I dont want to give a vehicleName when I try to create obj from LandVehicle –  Nov 25 '21 at 20:29
  • I guess what I want is not very suitable for OOP. I have to give a vehicleName while I creating new obj with constructor. But anyway I cant access vehicleName because it was private. This is how private modifiers used. –  Nov 25 '21 at 20:39
1

Generally, a class has a default constructor, taking no arguments, IF no constructor has been provided by you.

When you subclass Vehicle with your LandVehicle, your LandVehicle is a type of Vehicle. This means that it inherits methods and field from its superclass, even if they are private. For the class LandVehicle these members are just not visible, but they are still present - otherwise it couldn't function properly. The private keyword is an access modifier, that changes visibility to the caller.

As a result, to instantiate a LandVehicle, you also must provide the required attributes of its superclass Vehicle (since there is no default, no-arg constructor in Vehicle). In your example, a LandVehicle without a name (from Vehicle) wouldn't make sense, since a LandVehicle is a Vehicle, which requires a name.

davidrhp
  • 125
  • 1
  • 5