I have two classes, Item and SoldItem. SoldItem inherits from Item adding some property such as prize, solddate, etc.
First I create a Item, and when i sold it I want to create solditem. Is correct this design? if yes how I can create solditem starting from item?
I’d say this is improper use of inheritance. What if you need to diversify the
Iteminto subclasses by some feature that is orthogonal to whether it has been sold? For example, if you sell software, an item could be shipped on physical media, it could be downloadable, or it could be a subscription; if you decide to model these out by using inheritance as well, you already have six classes. And each orthogonal feature you add becomes a multiplier to the number of classes you need.A better design, involving composition, would be to have separate
ItemandSaleclasses, and a link between the two; then you can add functionality to theItemclass to get the relevantSale, if any, and vv., get theItemofSale(which should always be non-null). In this context, thinking relational rather than object-oriented is probably a better approach.If you are only interested in sold items, you’d look at
Saleobjects directly, getting item information through the relatedItem. If you are interested in any item, get anIteminstance, and if needed query it for aSale.