I have a base and sub class such as:
class BaseClass
{
public void MethodA()
{
MethodB();
}
public void MethodB()
{
Debug.Log("BaseClass MethodB");
}
}
class SubClass : BaseClass
{
public new void MethodB() // <- without `new` keyword there's a warning on this line
{
Debug.Log("SubClass MethodB");
base.MethodB();
}
}
When the MethodA
of the BaseClass instance is called it calls MethodB
but only of the BaseClass, and not of the SubClass first. e.g.
var subclass = new SubClass();
subclass.MethodA(); // Does not log "SubClass MethodB" first. Only logs "BaseClass MethodB"
How do you make sure the parent methods call the subclass methods?
Note, without the new
keyword on the MethodB
line, Visual Studio gives a warning: 'SubClass.MethodB()' hides inherited member 'BaseClass.MethodB()'. Use the new keyword if hiding was intended.