Reputation: 153
I have an abstract class that subclasses will inherit. I want to have a get
method that returns the amount of passengers. I declare this method in the abstract class and it will return the passengers.
The class member passengers
is not defined but instantiated, so the get
method knows what variable to return.
In the subclasses, I want this variable to have different values.
However, the get method returns 0
even when the amount of passengers is not 0.
I have tried writing: passengers = random.nextInt(4) + 1;
Abstract class vehicle
import java.util.Random;
public abstract class Vehicle {
protected Random random = new Random();
protected int passengers;
public int getPassengerAmount() {
return this.passengers;
}
}
Class Car
public class Car extends Vehicle {
private String name = "Car";
private int size = 1;
public int passengers = random.nextInt(4) + 1;
}
Upvotes: 2
Views: 114
Reputation: 2294
You are not using the good approach to calculate how many passengers are in a certain vehicle.
It's nice to have your Vehicle
as an abstract class as it will have only common caracteristic that all Vehicle
share and let the specificities of each one being implemented in their respective classes.
For the Car
. You have to implement your specific conception of how many passengers should be in a Car
. Thus, getPassengerAmount()
should be declared abstract
in your Vehicle
and be implemented in each specific vehicle.
That means :
import java.util.Random;
public abstract class Vehicle {
protected Random random = new Random();
protected int passengers;
public Vehicle() {
initializeVehiculeOptions();
}
public abstract int initializeVehiculeOptions();
}
and
import java.util.Random;
public class Car extends Vehicle {
private String name = "Car";
private int size = 1;
public Car() {
super();
}
@Override
public int initializeVehiculeOptions() {
// Here you initialize for
this.passengers = random.nextInt(4) + 1;
}
}
You will use a Car
instance
Car ford = new Car();
Upvotes: -1
Reputation: 1613
public class Car extends Vehicle {
private String name = "Car";
private int size = 1;
public int passengers = random.nextInt(4) + 1; // this is not passengers of the superclass
void setPassengers() { passengers = random.nextInt(4) + 1; } // this is passengers of the superclass
}
Upvotes: 0
Reputation: 183
The answer to your question will be that you are initializing passengers variable directly. you have to create a constructor for it and assign a default value there. plus there is no need for you to create a public variable again in the subclass otherwise the abstract class makes no sense of existing. Note, an abstract class must have an abstract function.
Upvotes: 0
Reputation: 4694
You are masking the parent variable by redeclaring the passengers
variable in the Car
class.
You should initialize the parent variable in the constructor of the child instead:
public class Car extends Vehicle {
private String name = "Car";
private int size = 1;
public Car() {
this.passengers = random.nextInt(4) + 1;
}
}
If you want it to be public, then you should make it public in the Vehicle
class, but to be honest I wouldn't recommend it and I would rather go with protected variables but public getters / setters.
Upvotes: 2