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
ProductandInventory, I quickly assumed that aProductis anInventorybecause aProductmust be inventorized as well. But a problem occured when user wanted to sell theirInventoryitem. It just doesn't seem to be right to change anInventoryobject to it's subtype (Product), it's almost like trying to convert a parent to it's child.Another case is
CustomerandMember. It is logical (at least for me) to think that aMemberis aCustomerwith some more privileges. Same problem occurred when user wanted to upgrade an existingCustomerto become aMember.A very trivial case is the
Employeecase. WhereManager,Clerk, etc can be derived fromEmployee. 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
Inventoryinside aProduct. Here I'm making an assumption about thatProductandInventoryis talking in a different context. WhileProductis in the context of sales (price, volume, discount, etc),Inventoryis in the context of physical management (stock, movement, etc).Make a reference of
Membershipinstead insideCustomerclass instead of previous inheritance solution. Therefor upgrading aCustomeris only about instantiating theCustomer'sMembershipproperty.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 abstractRoleclass and make it a property inEmployee.
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!