//Shadowing replaces the complete element of the parent.In simple word it can change a
//variable element of parent to a method or function
//Shadowing is useful when get a requirement where the vocabulary will be kept
//same but the data type needs to be change.
//Overriding changes implementation while Shadowing replaces element with a
//complete new element only the interface vocabulary is same
//Method Hiding
namespace MethodHiding
{
class A
{
public void PrintName()
{
Console.WriteLine("Print A");
}
}
class B : A
{
public new void PrintName()
{
Console.WriteLine("Print B");
}
}
class C : B
{
public new void PrintName()
{
Console.WriteLine("Print C");
}
}
class Program
{
static void Main(string[] args)
{
B objB = new B();
objB.PrintName();//B
C objC = new C();
objC.PrintName();//C
A objAB = new B();
objAB.PrintName();//A
B objCB = new C();
objCB.PrintName();//B
}
}
}
//Method Overriding
namespace MethodOverriding
{
class A
{
public virtual void PrintName()
{
Console.WriteLine("Print A");
}
}
class B : A
{
public override void PrintName()
{
Console.WriteLine("Print B");
}
}
class C : B
{
public override void PrintName()
{
Console.WriteLine("Print C");
}
}
class Program
{
static void Main(string[] args)
{
B objB = new B();
objB.PrintName();//B
C objC = new C();
objC.PrintName();//C
A objAB = new B();
objAB.PrintName();//B
B objCB = new C();
objCB.PrintName();//C
}
}
}
No comments:
Post a Comment