Is there any way to generate different types of objects from one method without using long and complicated if-statements?
Lets say I've created an abstract class called Vehicle. Vehicle has a constructor like this:
public abstract class Vehicle {
int mileage;
public Vehicle (int mileage) {
this.mileage = mileage;
}
}
Now, I've created lots and lots of different types of vehicles that all inherits Vehicle, such as DuneBuggy, SmallCar, MediumCar, BigCar, FastCar, OldCar, NewCar, PickUpTruck, SmallTruck, MediumTruck, LargeTruck, Bicycle, Tram, Train and so forth and so on. To my understanding they're now all Vehicles since they inherit Vehicle.
Is there any way I can get vehicle type from user together with mileage and create the correct type of vehicle by passing it to one method no matter what type of vehicle the user has selected? Something like this:
public void createNewVehicle (Variable someKindOfWayToPickVehicleType, int mileage) {
// Create a new vehicle where exact class is decided by someKindOfWayToPickVehicleType variable
}