extending a class

Posted on 2011-10-11
Last Modified: 2012-05-12
Say I run a used car dealership. So I have a class for Vehicle.

A new vehicle arrives, so I create an instance of the Vehicle class:
v = new Vehicle();

Open in new window

Then I'm told this vehicle is blue:
v.Color = blue;

Open in new window

Then I'm told this vehicle is a motorcycle with chrome handlebars. My Motorcycle class is an extension of the base class Vehicle, but now I have to abandon my Vehicle class v and create an instance of Motorcycle:
 m = new Motorcycle();

Open in new window

and copy over the color:
m.Color = v.Color;
m.handlebars = Chrome;

Open in new window

This doesn't seem right. I shouldn't have to abandon my vehicle class v and start a new class m. Yet it wouldn't be appropriate to have a "handlebar" field in my Vehicle class.

This could continue. Say I'm then told this is a Kawasaki. Now I have to abandon my m instance of Motorcycle, create an instance of class Kawasaki which extends class Motorcycle, and copy over all the info once again.

What I'm really doing is receiving a message, which I thought I'd create an instance of a Message class and put the message in it. Only problem is there are several different types of messages, I don't know what type of message I have until I parse it, but it seems from a design point of view that the parsing should be done inside the Message class, but I don't know what kind of Message class instance to create until I parse the message. Catch 22.

How does one deal with this from a design point of view?
Question by:deleyd
    LVL 7

    Accepted Solution

    You are right; you shouldn't be re-creating the class.

    I don't know your exact code or data flow, but in general you might consider:

    1. Create base class for vehicle, and derive all from it.  E.g.,:

    public class Motorcycle : Vehicle { // stuff }

    2. create an enum of vehicle types. e.g.:
    enum Vehicles
    3. create enum's of sub-components for each vehicle type, if necessary.  You may want to use bitfields if you plan on supporting more than one at a time.  E.g.:

    enum Parts_Unicycle
       Wheel = (1 << 0),
       RocketLauncher = (1 << 1)
    4. parse your message and gather all your info.
    5. Create the appropriate class and assign values after you've collected all data.  E.g.:

    Vehicle h = null;
    switch (typeVehicle)
       case Vehicles.Unicycle:
          v = new Motorcycle();
          // assign parts, etc...
    LVL 7

    Expert Comment

    oops, sample at end should have shown:

    v = new Unicycle();
    LVL 7

    Expert Comment

    Also, if you actually have to support a system where the vehicle type/integrity DOES change as you go, then either:

    1. Store all info to DB.  Create appropriate vehicle at run time from this data.
    2. If all must be done at run time (changing classes), consider creating one vehicle class that contains all potential info (including the type of vehicle, a parts list [each part in list derived from a base class].)

    Author Closing Comment

    OK so there's no simple remedy I'm missing. I either create one class which contains all message types and possible message info fields, or I parse the message first, then create the appropriate class to store the already parsed info in.
    LVL 7

    Expert Comment

    If you set this framework up carefully, taking care to recognize any constituent elemental parts, etc., that may have class-specific implementations (e.g., class RocketLauncher derives from class Part) you will find that you can easily accommodate anything new, at the same time as avoiding a host of other potential issues.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
    Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    794 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now