So both the player as well as the monsters in my game are Creatures. I’ve simplified each class to highlight the issue. Here is the creature class:
Each creature can be assigned a different AI, so that the player will be controlled by input and the monsters will have their own AI etc. There is a base CreatureAI and the player as well as each monster will have it’s own ai class.

Here is the PlayerAI class for example (inheriting from CreatureAI)
Here is the main class where we set up the creature and assign it an AI, then call the notify method on it.

This is the output:
So in the Creature.Notify when we log it KNOWS that the ai is of type PlayerAI, yet it still only calls CreatureAI.OnNotify() instead of PlayerAI.OnNotify(). Can anyone explain why that is?
I could get around it by changing the code and forcing Creature.Notify() method to call PlayerAI like this:

But I’ll have to have a massive switch statement for all the different classes if I’m going to do it that way and I’d rather not do that. Is there a solution which keeps things more or less ‘elegant’?
Add that “virtual” key word to the base notify method. Add the override key word to the derived notify method.
thanks this worked !

It seems like what you want is to be able to override the OnNotify method in your PlayerAI class with an implementation that is different from its parent. Fortunately there is a way to do this, and it’s just a matter of the keywords you use in the method declarations.

To override a method, it has to be marked as either abstract or virtual in the base class. Since you have a default implementation, you want to use virtual as abstract methods can’t have bodies. So in CreatureAI :
Then in the derived class (PlayerAI) instead of the “new” keyword you need the “override” keyword:

The reason your code doesn’t work the way you want is the “new” keyword. When you use it, that method is no longer attached to the class inheritance chain, and is an entirely new separate method. And since the reference to the AI object is stored in Creature as type CreatureAI, the CreatureAI OnNotify method is the only one that can be called.
Edit: formatting
thanks virtual and override worked!

The “new” keyword doesn’t do anything. It just acknowledges to the compiler you know what you’re doing. It hides the base method when called via the new type.

You’re still calling it via the old type, since your Creature class accepts a CreatureAI object. Since it calls it by the base type, the base method is called.
What you want is method overrides, not “new”.
Define the base method as virtual and in the sub class override it.
An override, as the name implies, overrides the method defintion. It directly replaces it as far as the type is concerned. This override will persit whether you call it by the subclass type, or any baseclass type or interface. (As long as they still have it)

When using an override, you can still call the base method that you’ve overriden with the base keyword, just like with ctors.
thanks! that worked!
awesome

source