Thursday, September 3, 2009

How to do implement override in C#

image
class SuperClass
{
    public virtual void MyMethod() 
    { // Code Here }
}
 
class SubClassA : SuperClass
{
    public override void MyMethod()
    { // Code Here }
}
 
class SubClassB : SubClassA
{
    public override void MyMethod()
    { // Code Here }
}

Override is method of implementing polymorphism. Though three classes have same method they will show a unique behavior. as an example,


SuperClass A = new SuperClass();
A.MyMethod();
// This will execute code defined in SuperClass

A = new SubClassA();
A.MyMethod();
// This will execute code defined in SubClassA

Read More

Post a Comment