using System;
using System.Text;
namespace Polymorphism
{
class Program
{
static void Main(string[] args)
{
Derived1 dv1 = new Derived1();
//Do you know which Class Method is called here ?
dv1.Method(dv1);
Derived2 dv2 = new Derived2();
//Do you know which Class Method is called here ?
dv2.Method(dv2);
DerivedDerived3 dv3 = new DerivedDerived3();
//Do you know which Class Method is called here ?
dv3.Method(dv3);
DerivedDerived4 dv4 = new DerivedDerived4();
//Do you know which Class Method is called here ?
dv4.Method(dv4);
Console.ReadLine();
}
}
class Based1
{
public string Display()
{
return "based";
}
}
class Derived1:Based1
{
//What is the use of new keyword ?
public new string Display()
{
return "derived";
}
public void Method(Based1 t)
{
Console.WriteLine(t.Display());
}
}
class Based2
{
//What is the use of virtual keyword ?
public virtual string Display()
{
return "based";
}
}
class Derived2 : Based2
{
//What is the use of override keyword ?
public override string Display()
{
return "derived";
}
public void Method(Based2 t)
{
Console.WriteLine(t.Display());
}
}
class Based3
{
public string Display()
{
return "based";
}
}
class Derived3 : Based3
{
//What is the use of 'virtual new' keyword ?
//Methods of a derived class can both be virtual and at the same time hide the derived method.
//In order to declare such a method, both keywords virtual and new have to be used in the method declaration
public virtual new string Display()
{
return "derived";
}
}
class DerivedDerived3 : Derived3
{
public override string Display()
{
return "derivedderived";
}
public void Method(Derived3 t)
{
Console.WriteLine(t.Display());
}
}
class DerivedDerived4 : Derived3
{
public new string Display()
{
return "derivedderived";
}
public void Method(Derived3 t)
{
Console.WriteLine(t.Display());
}
}
}
using System.Text;
namespace Polymorphism
{
class Program
{
static void Main(string[] args)
{
Derived1 dv1 = new Derived1();
//Do you know which Class Method is called here ?
dv1.Method(dv1);
Derived2 dv2 = new Derived2();
//Do you know which Class Method is called here ?
dv2.Method(dv2);
DerivedDerived3 dv3 = new DerivedDerived3();
//Do you know which Class Method is called here ?
dv3.Method(dv3);
DerivedDerived4 dv4 = new DerivedDerived4();
//Do you know which Class Method is called here ?
dv4.Method(dv4);
Console.ReadLine();
}
}
class Based1
{
public string Display()
{
return "based";
}
}
class Derived1:Based1
{
//What is the use of new keyword ?
public new string Display()
{
return "derived";
}
public void Method(Based1 t)
{
Console.WriteLine(t.Display());
}
}
class Based2
{
//What is the use of virtual keyword ?
public virtual string Display()
{
return "based";
}
}
class Derived2 : Based2
{
//What is the use of override keyword ?
public override string Display()
{
return "derived";
}
public void Method(Based2 t)
{
Console.WriteLine(t.Display());
}
}
class Based3
{
public string Display()
{
return "based";
}
}
class Derived3 : Based3
{
//What is the use of 'virtual new' keyword ?
//Methods of a derived class can both be virtual and at the same time hide the derived method.
//In order to declare such a method, both keywords virtual and new have to be used in the method declaration
public virtual new string Display()
{
return "derived";
}
}
class DerivedDerived3 : Derived3
{
public override string Display()
{
return "derivedderived";
}
public void Method(Derived3 t)
{
Console.WriteLine(t.Display());
}
}
class DerivedDerived4 : Derived3
{
public new string Display()
{
return "derivedderived";
}
public void Method(Derived3 t)
{
Console.WriteLine(t.Display());
}
}
}
No comments:
Post a Comment