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.
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
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.
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.
Excellent .. I got a clear understanding of C# concepts .Thank u very much Venkat for these great videos.
ReplyDeletethank you for nice explaination
ReplyDeletethank you..nice information.... send me more blogs.... that you know about c# my email is julian.ramirez@dracobots.com
ReplyDeleteHello Venkat,
ReplyDeleteI 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.
1) a = new B();
DeleteTest() 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.
what if we use a = new C();
Deletea.Test(); // output --> "A::Test()"
Can anyone explain this behavior please?
It should have given C::Test() as the method is overridden
In above code, you have use new keyword in class B(). So when we use new keyword it hide base class(B) method .
ReplyDeleteDefinitely one of the best training resources, the concepts are very well and in simple terms explained.
ReplyDeleteHello Venkat i am fan of your tutorials.
ReplyDeleteThis 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.
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.
ReplyDeleteMy 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.