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
andInventory
, I quickly assumed that aProduct
is anInventory
because aProduct
must be inventorized as well. But a problem occured when user wanted to sell theirInventory
item. It just doesn't seem to be right to change anInventory
object to it's subtype (Product
), it's almost like trying to convert a parent to it's child.Another case is
Customer
andMember
. It is logical (at least for me) to think that aMember
is aCustomer
with some more privileges. Same problem occurred when user wanted to upgrade an existingCustomer
to become aMember
.A very trivial case is the
Employee
case. 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
Inventory
inside aProduct
. Here I'm making an assumption about thatProduct
andInventory
is talking in a different context. WhileProduct
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 insideCustomer
class instead of previous inheritance solution. Therefor upgrading aCustomer
is only about instantiating theCustomer
'sMembership
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 abstractRole
class 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!