I have this class model where, Bank is a class which is now going for computerized Banking network. This must have ATM(Automatic Teller Machine) and also Human Cashier.
I used Generalization and have taken a class called AccountHandlers which inherits Bank class. This AccountHandlers further have ATM and HumanCashier aggregated to it.
Now the thing is, my friend was arguing that i have taken the whole thing wrong. According to him AccountHandlers must be aggregated to Bank and that ATM and HumanCashier must inherit to AccountHandlers.
I am a bit confused over it. How can i model it!! or is that both method correct?
I would go back to the basics.
You should ask yourself if an ATM
is an AccountHandler
, or if an AccountHandler
has an ATM
. That should give you a general answer as to the question of using inheritance or composition.
Both would be correct. Only one would be a good design and that is dependent on what your application is trying to do.
Generally, there is a rule of thumb (taken from Effective Java) that states that you should favor composition over inheritance. Take that with a grain of salt, and make sure you are designing your app the right way. (For more info see Prefer composition over inheritance?)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With