OOP concept: is it possible to update the class of an instantiated object?

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

2

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.

2

No, you can’t change the class of an object once instantiated, at least not in any programming language I know of. The reason is that this would blow up all sorts of guarantees a class hierarchy would otherwise give you; not to mention that “changing the class of an object” is a highly undefined requirement – how do you migrate the object’s state? How do you map fields from one class to the other?

Considering your problem at hand; you have fallen prey to the seduction of naive polymorphism and reuse-by-inheritance. While both a Car and a Truck are vehicles, it is not very wise to have them inherit from Vehicle directly and encode all the behavior in these three classes. In other news, the textbook is wrong.

Instead, split the functionality into components. A Vehicle has a license plate, and a type. The type determines whether it is a car or a truck. You can then swap out the vehicle’s type without changing its base properties.

Also, note that you are probably lying about your data. You create a Vehicle object without actually having a vehicle at hand; all you want is store the license plate number. So maybe putting the license plate into its own class, and linking it to an actual vehicle later might be a better design choice.

8

I would use a different design where garage contains a list of parkingspace objects.
The parkingspace object would implement the interface Licensplate.

Now I would have the vehicle object also implement the licensplate interface.

That way you can instantiate your list of parkingspaces, with corrensponding licenseplate numbers and instantiate your cars and trucks later with licensplates.

probably you are looking for this pattern

you have to tweak this code to your exact requirement.

public interface IVehicle
{  
    string VehicleType{get;}
    int NumberOfSeets{set;get;}
    int Corgosize{set;get;}
}

public class Car:IVehicle
{
    private int _numseets;
    private int _corgo;
    public string VehicleType{
        get{return "CAR";}
    }

    public int NumberOfSeets
    { 
       get{return _numseets;}
       set{_numseets = value;}
    }

    public int Corgosize
    {
        get { return _corgo; }
        set { _corgo = value; }
    }
}

public class Truck : IVehicle
{
    private int _numseets;
    private int _corgo;
    public string VehicleType
    {
        get { return "Truck"; }
    }

    public int NumberOfSeets
    {
        get { return _numseets; }
        set { _numseets = value; }
    }

    public int Corgosize
    {
        get { return _corgo; }
        set { _corgo = value; }
    }
}

public class Vehicle : IVehicle
{
    private IVehicle _vehicle;
    public Vehicle(IVehicle refVehicle)
    {
        _vehicle = refVehicle;
    }
    public string VehicleType
    {
        get { return _vehicle.VehicleType; }
    }

    public int NumberOfSeets
    {
        get { return _vehicle.NumberOfSeets; }
        set {  }
    }

    public int Corgosize
    {
        get { return _vehicle.Corgosize; }
        set {  }
    }

}

public class MainClass
{
    public static void Main()
    {
        Vehicle car, truck;
        car = new Vehicle(new Car());
        truck = new Vehicle(new Truck());
        Console.WriteLine("Car type ->{0}",car.VehicleType);
        Console.WriteLine("truck type ->{0}", truck.VehicleType);
        Console.ReadKey();

    }

}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật