I know I might get downvoted for this, but I'm really curious.
I was taught that inheritance is a very powerful polymorphism tool, but I can't seem to use it well in real cases.
So far, I can only use inheritance when the base class is an abstract class.
If we're talking about
Inventory, I quickly assumed that a
Productmust be inventorized as well. But a problem occured when user wanted to sell their
Inventoryitem. It just doesn't seem to be right to change an
Inventoryobject to it's subtype (
Product), it's almost like trying to convert a parent to it's child.
Another case is
Member. It is logical (at least for me) to think that a
Customerwith some more privileges. Same problem occurred when user wanted to upgrade an existing
Customerto become a
A very trivial case is the
Clerk, etc can be derived from
Employee. Still, the same upgrading issue.
I tried to use composition instead for some cases, but I really wanted to know if I'm missing something for inheritance solution here.
My composition solution for those cases :
Create a reference of
Product. Here I'm making an assumption about that
Inventoryis talking in a different context. While
Productis in the context of sales (price, volume, discount, etc),
Inventoryis in the context of physical management (stock, movement, etc).
Make a reference of
Customerclass instead of previous inheritance solution. Therefor upgrading a
Customeris only about instantiating the
This example is keep being taught in basic programming classes, but I think it's more proper to have those
Clerk, etc derived from an abstract
Roleclass and make it a property in
I found it difficult to find an example of a concrete class deriving from another concrete class.
Is there any inheritance solution in which I can solve those cases?
Being new in this OOP thing, I really really need a guidance.