Search code examples
c#virtualabstract

override an abstract function with a virtual one?


Basically i'm asking how best to do this properly based on what I THINK I want.

I have a weapon class which is abstract because it contains methods like Fire() that are so general (lasers vs bullets) in how they'd fire they require the inheritor implement.

however once I get to an actual ballistic weapon class which inherits from weapon class.

well most of those fire the same. They spawn the projectile and lower the ammo count and wait for the reload timer to kick down to 0 before allowing fire again.

Whether it's a tank gun or a pistol or a turret or an artillery it will stay the same more than change.

That seems like a virtual class (a torpedo launcher might need a special implementation to raise and lower a hatch door to fire so it can't be non-virtual/abstract, it needs to be modifiable)

however basically I want to have

abstract weapon class Fire() method

virtual ballistic weapon inherits weapon Fire() method

But it doesnt seem that you can override an abstract method and make it a virtual one.

I don't think I want a whole new method though because i'd like to be able to do

weapon.fire

and it go into ballstic weapon and use the implementation there, unless the specific ballistic weapon has overridden that general method.


Solution

  • Abstract means it must be overridden in child class where virtual means it can be overridden. You can have virtual methods in an abstract class. You can solve your problem using Interfaces by binding in a form of contract like:

    Ifireable