Overriding in C # by making two subsequent methods virtual

This is a matter of overriding in C #.

When I use the following code:

class Program
{


    class A
    {
        public virtual void callme()
        {
            Console.WriteLine("this is A");
        }
    }
    class B : A
    {
        public new virtual void callme()
        {
            Console.WriteLine("this is B");
        }
    }
    class C : B
    {
        public override void callme()
        {
            Console.WriteLine("this is C");
        }
    }


    static void Main(string[] args)
    {
        A obj = new C();
        obj.callme();
        Console.ReadKey();
    }
}

      

OUTPUT: This is A

And when I use:

class Program
{


    class A
    {
        public virtual void callme()
        {
            Console.WriteLine("This is A.");
        }
    }
    class B : A
    {
        public override void callme()
        {
            Console.WriteLine("This is B.");
        }
    }
    class C : B
    {
        public override void callme()
        {
            Console.WriteLine("This is C.");
        }
    }


    static void Main(string[] args)
    {
        A obj = new C();
        obj.callme();
        Console.ReadKey();
    }
}

      

OUTPUT: This is C.

So if I make the method virtual in subsequent classes ( A

as well as B

), why does it call the last method, and if the class B

overrides A

and C

is equal to overriding B

, then it calls the method C

.

Explain, please.

+3


source to share


3 answers


In the first case, B shades A and C overrides the shadow in B. So as long as the reference type is A, it will call A.

In the second case, B overrides A and C overrides B (so it overrides A).



See: Differences Between Shadows and Overrides

+2


source


Check the compiler warning:

Warning 1 '..Program.B.callme ()' hides the inherited element '..Program.A.callme ()'. To have the current member override this implementation, add the override keyword. Otherwise, add a new keyword ... \ Program.cs 44 33 Testing



It states that a method in B hides a method in A. When you override a method in C; it just overrides the method in B not in (the compiler assumes you're callme

in class B - the new method). So when you call A.callme

, since method in is not overridden, it returns "this is A".

+1


source


... so if I make the method virtual in subsequent classes (A as and B), why does it call the last method, and if class B overrides and C overrides, then its call C

In the first example, you are just replacing methods in classes B and C. So when you update your instance to class A, it just uses the method in class A.

In the second part, you use methods overriding

and use polymorphism to change the behavior of a method in classes B and class C. When you override a method, that method is not replaced, but constantly changes. Whenever you call this method again, it will call the method with the override keyword.

More information: https://msdn.microsoft.com/en-us/library/ebca9ah3.aspx

PS In short - if you have To replace the method with a new one:

class A { MyMethod() {} }       Class B : A { MyMethod() {} }

      

To override that all subsequent calls (even in the parent class) will use override logic:

class A { virtual MyMethod() {} }      Class B : A { override MyMethod() {} }

      

-1


source







All Articles