user420667
user420667

Reputation: 6700

method resolution with base types

My situation is this:

public class InheritedClass : BaseClass
{
    public override void SomeMethod()
    {
        AnotherMethod();
    }
    public override void AnotherMethod()
    {
    }
}

public class BaseClass
{
    public virtual void SomeMethod()
    { }
    public virtual void AnotherMethod()
    { }
}

So which method is called when I call InheritedClassInstance.SomeMethod? Does it call InheritedClassInstance.AnotherMethod, or the BaseClass's AnotherMethod?

Upvotes: 0

Views: 96

Answers (2)

David Neale
David Neale

Reputation: 17028

It will call the derived method on the inherited class unless you explicitly call the base method (base.AnotherMethod())

Upvotes: 0

hunter
hunter

Reputation: 63522

It calls InheritedClassInstance.AnotherMethod()

If you wanted it to call the base class AnotherMethod() you would write base.AnotherMethod()

Upvotes: 2

Related Questions