Support us .Net Basics C# SQL ASP.NET ADO.NET MVC Slides C# Programs Subscribe Buy DVD

Part 24 - C# Tutorial - Difference between method overriding and method hiding


Part 24 - C# Tutorial - Difference between method overriding and method hiding


8 comments:

  1. Excellent .. I got a clear understanding of C# concepts .Thank u very much Venkat for these great videos.

    ReplyDelete
  2. thank you for nice explaination

    ReplyDelete
  3. thank you..nice information.... send me more blogs.... that you know about c# my email is julian.ramirez@dracobots.com

    ReplyDelete
  4. Hello Venkat,
    I have one doubt regarding method overring and method hiding. Please refer below example.

    using System;
    namespace Polymorphism
    {
    class A
    {
    public void Test() { Console.WriteLine("A::Test()"); }
    }

    class B : A
    {
    public new virtual void Test() { Console.WriteLine("B::Test()"); }
    }

    class C : B
    {
    public override void Test() { Console.WriteLine("C::Test()"); }
    }

    class Program
    {
    static void Main(string[] args)
    {

    A a = new A();
    B b = new B();
    C c = new C();

    a = new B();
    a.Test(); // output --> "A::Test()"

    b = new C();
    b.Test(); // output --> "C::Test()"

    Console.ReadKey();
    }
    }
    }

    With method hiding, "a = new B()" returns "A::Test()" and
    with method overriding "b = new C()" returns "C::Test()".

    I dont understand this behaviour. Can you please help me with this.

    ReplyDelete
    Replies
    1. 1) a = new B();
      Test() method of Class A is hiding because Test() method of Class B was declared with new keyword, it means in method hiding when we use new keyword the base class method will be hide and child class method will be executed.

      2) b = new C();
      Test() method of Class B is declared as virtual and Test() method of Class C is declared as overridden with override keyword. It means in method overriding when we use virtual keyword it will refer to the child class whether or not overridden. If so it will be executed from the child classes.

      Delete
  5. In above code, you have use new keyword in class B(). So when we use new keyword it hide base class(B) method .

    ReplyDelete
  6. Definitely one of the best training resources, the concepts are very well and in simple terms explained.

    ReplyDelete
  7. Hello Venkat i am fan of your tutorials.

    This video helps is understanding concepts of overriding. But at the same time i dont understand the practical meaning. Why to use overriding. Why would one want to create a base class reference variable to refer to a derived class and then call derived class method. Why not simply create a derived class method.

    I have been digging for a real time scenario but couldnt find anything satisfactory.

    ReplyDelete

If you like this website, please share with your friends on facebook and Google+ and recommend us on google using the g+1 button on the top right hand corner.