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
ATMis anAccountHandler, or if anAccountHandlerhas anATM. 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?)