I have an abstract class that we will call Vehicle. Classes Diesel, Electric, and Gas extend Vehicle. Those 3 classes have their own methods and variables, as well as what they inherited from Vehicle.
I want to put variables from the Diesel, Electric, and Gas objects into the same table within a database. A record created from a Electric object would simply have a few empty columns where it didn't have the same variables as the Diesel object did, etc.
I want to make methods to insert rows, update rows, and retrieve rows, all accepting objects from these classes as arguments.
I am searching for the best solution to this problem, as I do not think that I know what it is. So far I can think of a few solutions, but I do not like any of them:
Create 4 of each of the following methods: insert, update, delete, and retrieve/select. This would result in a lot of duplicate code.
public void AddVehicle(Diesel diesel) public void AddVehicle(Electric electric) public void AddVehicle(Gas gas) public Electric ViewVehicles() public Diesel ViewVehicle() public Gas ViewVehicle() public void RemoveVehicle(Diesel diesel) public void RemoveVehicle(Electric electric) public void RemoveVehicle(Gas gas) public void UpdateVehicle(Diesel diesel) public void UpdateVehicle(Electric electric) public void UpdateVehicle(Gas gas)
Create each method so that it can accept any object: (Rather than taking any object, is there a way to specify a list of objects which it can take, but still only take one of them? ie. public void AddVehicle (Diesel diesel || Electric electric || Gas gas))
public void AddVehicle(Object vehicle) public Object ViewVehicle(Object vehicle) public void RemoveVehicle(Object vehicle) public void UpdateVehicle(Object vehicle)
Create a "master" class that can contain any of the classes (Diesel, Electric, or Gas) which is then passed into the insert/update/view/delete methods. (Setting the insert/update/view methods up to accept this "master" class as an argument.) Incomplete constructors for the "master" class:
public MasterVehicle(Diesel diesel) public MasterVehicle(Electric electric) public MasterVehicle(Gas gas)
Some other solution which is proper, cleaner, and makes more sense.
But what might #4 be?