Imagine that I am writing a game where tanks fight with each other.
A generic Tank class is created and has the method fire() which fires a cannon, looks like this Tank::fire() { /* fires a cannon */ }
And then there is a class for BossTank which fire() fires a cannon and also a missile.
A senior advised me to do something like:
Tank::fire() { fireProjectile(); }, and create a new function:
Tank::fireProjectile() { /* fire a cannon */ }
Then in BossTank we can do inherit:
BossTank::fireProjectile() {
parent::fireProjectile();
/* fire a missile */
}
I don't know why, I do not feel good about this. Maybe because the line parent::fire() looks inconsistent with more elementary lines for /* fire a missile */
Another coworker suggested this:
Tank::fire() { fireCannon(); } and Tank::fireCannon() { /* fire a cannon */ }
Then BossTank::fire() { fireCannon(); fireMissile(); } with BossTank::fireMissile { /* fire a missile */ } (this is a method only in this class)
I would like to go along these lines:
Tank::fire() { fireCannon(); fireAdditionalProjectiles(); } with Tank::fireAdditionalProjectiles() empty.
Then in BossTank we can override fireAdditionalProjectiles() to fire missiles.
So here are 3 ways to implement the "same" thing:
- Senior: Inheritance - extracting fire cannon so no duplication.
- Coworker: Override
fire()and implement a new function only in BossTank - Me: Implement new function
fireCannon()for no duplication, andfireAdditionalProjectiles()for overriding in BossTank
Is this a personal preference or there are standards regarding this situation?