Support us .Net Basics C# SQL ASP.NET Aarvi MVC Slides C# Programs Subscribe Download

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


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


10 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
    2. what if we use a = new C();
      a.Test(); // output --> "A::Test()"
      Can anyone explain this behavior please?
      It should have given C::Test() as the method is overridden

      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
  8. Hello venkat garu you are my online dot net guru. I like all your videos. you are donating your knowledge by giving excellent examples. your knowledge in dot net looks like you designed the entire dot net framework. no other trainers taught like you.

    My question: when we are creating child class by using new keyword we can hide the child class method. give a scenario where we need to hide the child class methods. please
    thank you.

    ReplyDelete

It would be great if you can help share these free resources