OOP Concepts – Is It Possible to Update the Class of an Instantiated Object?

inheritanceobject-orientedvb.net

I am trying to write a simple program that should allow a user to save and display sets of heterogeneous, but somehow related data. For clarity sake, I will use a representative example of vehicles. The program flow is like this:

  1. The program creates a Garage object, which is basically a class that can contain a list of vehicles objects
  2. Then the users creates Vehicles objects, these Vehicles each have a property, lets say License Plate Nr. Once created, the Vehicle object get added to a list within the Garage object
  3. Later on–, the user can specify that a given Vehicle object is in fact a Car object or a Truck object (thus giving access to some specific attributes such as Number of seats for the Car, or Cargo weight for the truck)

At first sight, this might look like an OOP textbook question involving a base class and inheritance, but the problem is more subtle because at the object creation time (and until the user decides to give more info), the computer doesn't know the exact Vehicle type.

Hence my question: how would you proceed to implement this program flow? Is OOP the way to go?

Just to give an initial answer, here is what I've came up until now. There is only one Vehicle class and the various properties/values are handled by the main program (not the class) through a dictionary. However, I'm pretty sure that there must be a more elegant solution (I'm developing using VB.net):

Public Class Garage
    Public GarageAdress As String
    Private _ListGarageVehicles As New List(Of Vehicles)

    Public Sub AddVehicle(Vehicle As Vehicles)
        _ListGarageVehicles.Add(Vehicle)
    End Sub
End Class

Public Class Vehicles
    Public LicensePlateNumber As String
    Public Enum VehicleTypes
        Generic = 0
        Car = 1
        Truck = 2
    End Enum
    Public VehicleType As VehicleTypes
    Public DictVehicleProperties As New Dictionary(Of String, String)
End Class

NOTE that in the example above the public/private modifiers do not necessarily reflect the original code

Best Answer

No, there's no way to change the instantiated type of an object. A Vehicle is a Vehicle, and will remain such until it goes out of scope.

Your choices here are threefold:

  1. Make the type of Vehicle simply a property on the Vehicle object, and default it to Unknown.
    • This works best when there's little functional difference between a Car and a Truck.
    • Functions may return different results depending on the VehicleType, and you'll need to handle the Unknown case, but it's trivial to reassign.
  2. Replace the Vehicle with a new object of Car or Truck
    • This would require either a IsActuallyACar() method on the Vehicle which returns a Car, or a Car constructor which takes a Vehicle as an argument (which is the much better choice).
    • Going with this method requires you to manually remove the Vehicle from everywhere that it's stored, and replace it with the newly created object.
  3. Rethink your design so that Vehicle is abstract and you don't create the object until you know what type it is.
    • This is probably the best option from an OO perspective.
    • You may need to temporarily store data in a non-Vehicle structure as you gather it, but you don't need to worry about changing the type later.

In your described scenario, I suspect #1 is the best choice. Simply have logic which says "If the object has its type set to Car, then show this, if it's set to Truck show this." It's not great OOP design, but it handles the "Vehicle of unknown type" the best.

Related Topic