Confusion about inheritance
- by Samuel Adam
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.
Examples :
If we're talking about Product and Inventory, I quickly assumed that a Product is an Inventory because a Product must be inventorized as well. But a problem occured when user wanted to sell their Inventory item. It just doesn't seem to be right to change an Inventory object to it's subtype (Product), it's almost like trying to convert a parent to it's child.
Another case is Customer and Member. It is logical (at least for me) to think that a Member is a Customer with some more privileges. Same problem occurred when user wanted to upgrade an existing Customer to become a Member.
A very trivial case is the Employee case. Where Manager, 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 Inventory inside a Product. Here I'm making an assumption about that Product and Inventory is talking in a different context. While Product is in the context of sales (price, volume, discount, etc), Inventory is in the context of physical management (stock, movement, etc).
Make a reference of Membership instead inside Customer class instead of previous inheritance solution. Therefor upgrading a Customer is only about instantiating the Customer's Membership property.
This example is keep being taught in basic programming classes, but I think it's more proper to have those Manager, Clerk, etc derived from an abstract Role class and make it a property in Employee.
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.
Thanks!