Robert W
Robert W

Reputation: 2961

Override an overridden method (C#)

I'm trying to override an overridden method (if that makes sense!) in C#.

I have a scenario similar to the below, but when I have a breakpoint in the SampleMethod() in the "C" class it's not being hit, whilst the same breakpoint in the "B" method is being hit.

public class A
{
      protected virtual void SampleMethod() {}
}

public class B : A 
{
      protected override void SampleMethod()
      {
           base.SampleMethod(); 
      }
}

public class C : B
{
      protected override void SampleMethod() 
      {
           base.SampleMethod(); 
      }
}

Thanks in advance!


Edit:

Ok, the context would help:

This is in the context of a composite control so class A inherits from CompositeControl and calls SampleMethod() after overriding the CreateChildControls() method.

Upvotes: 50

Views: 49531

Answers (6)

altamimi.morad
altamimi.morad

Reputation: 21

for Overriding more than once in the hierarchy use something like this

// abstract class
abstract class A
    {
        public abstract void MethodOne();
    }

// class B inherits A
class B : A
{
    public override void MethodOne()
    {
        Console.WriteLine("From Class B");
    }
}

// class C inherits B
class C : B
{
    public override void MethodOne()
    {
        Console.WriteLine("From Class C");
    }
}

// class D inherits C
class D : C
{
    public override void MethodOne()
    {
        Console.WriteLine("From Class D");
    }
}

// etc......

// class Main method Class

class MainClass
{
    public static void Main()
    {
        B[] TestArray = new B[3];
        B b1 = new B();
        C c1 = new C();
        D d1 = new D();

        TestArray[0] = b1;
        TestArray[1] = c1;
        TestArray[2] = d1;

        for (int i = 0; i < TestArray.Length; i++)
        {
            TestArray[i].MethodOne();
        }

        Console.ReadLine();
    }
}

I did it in this code in this link http://www.4shared.com/rar/0SG0Rklxce/OverridingMoeThanOnce.html

Upvotes: 2

Ahmad Salam Mehsud
Ahmad Salam Mehsud

Reputation: 7

Method overriding is OOP feature that allows a child class to provide a specific implementation of a method that is already provided by one of its parent classes.

Upvotes: -3

Mike J
Mike J

Reputation: 3159

The breakpoint is more than likely not being hit because you actually instantiated an instance of the "B" class.

Method override resolution works based on the actual runtime type of the class whose method should be called. So, if you had the following code:

C c = new C();
c.SampleMethod();

and the following:

C c = new C();
B b = (B)c;
b.SampleMethod();

both the runtime types of the class whose SampleMethod will be called is type B.

Upvotes: 4

Callum Rogers
Callum Rogers

Reputation: 15829

That solution works fine; although to actually use it outside the class the method is in, you need to set the access of SampleMethod to public rather than protected in all of the cases it appears, so:

public class A
{
    public virtual void SampleMethod() 
    {
        Console.WriteLine("lol");
    }
}

public class B : A
{
    public override void SampleMethod()
    {
        base.SampleMethod();
    }
}

public class C : B
{
    public override void SampleMethod()
    {
        base.SampleMethod();
    }
}

Upvotes: 2

Noldorin
Noldorin

Reputation: 147471

Overriding can be performed in a chain as long as you like. The code you have shown is correct.

The only possible explanation for the behaviour you are seeing is that the object to which you are referring is actually of type B. I suggest that you double check this, and if things still don't make sense, post the other appropiate code.

Upvotes: 52

Colin Mackay
Colin Mackay

Reputation: 19185

Without seeing the code that calls SampleMethod, my guess would be that you have an object of type B and call SampleMethod on that.

Upvotes: 22

Related Questions