Composition instead of inheritance c# | Sololearn: Learn to code for FREE!


Composition instead of inheritance c#

I have a class Monster { move() attack() } we can create: melee monster : monster overr attack() ranged monster : monster ovverr attack() flying monster : monster overr move() swimming monster : monster overr move() how to create FlyingRangeMonster and MeleeSwimmingMonster without duplicating code? I suppose we should abandon inheritance and use composition. How to realízate it in this case ? Thank you!

3/12/2021 6:49:01 AM


2 Answers

New Answer


How about an interface monster with methods move and attack


but if we create an interface we will duplicate code (e.g. melee monster and swimmingmeleemonster will have same realization of attack method)