Search This Blog

2009-12-01

Tutorial 8:Polymorphism

A virtual method in C# specifies an implementation of a method that can be polymorphicaly overridden derived method. A non-virtual method can’t be polymorphically override in a Derived class.

A virtual method can be declared by using the keyword virtual as follows.

class Base
{
public virtual void Method()
{
}
}

When we declare a virtual method, it must contain a method body. Other wise the compiler will generate an error. Remember that, since virtual methods are used for achieving polymorphism and since polymorphism works only with objects, it not possible to declare a static method as virtual in C#. Similarly the private methods are also not possible to declare virtual, since they can’t override inside a derived class.

In C#, it is not necessary to override a Base class virtual method inside a Derived class.

The following program will work absolutely correct.

// Inheritance : Virtual methods

using System;
class Base
{
public virtual void Method()
{
Console.WriteLine("Base method");
}
}
class Derived : Base
{
}
class MyClient
{
public static void Main()
{
Derived d1 = new Derived();
d1.Method(); // Displays 'Base Method'
}
}

Or even it is possible to override a virtual method non-polymorphically inside a Derived class as shown below.

// Inheritance : Virtual methods

using System;
class Base
{
public virtual void Method()
{
Console.WriteLine("Base method");
}
}
class Derived : Base
{
public virtual void Method()
{
Console.WriteLine("Derived method");
}
}
class MyClient
{
public static void Main()
{
Derived d1 = new Derived();
d1.Method(); // Displays 'Derived Method'
}
}

Even it is possible to omit the keyword virtual from the Derived class method or it is possible to declare the Derived class method as new.

When we want to override a virtual method polymorphically inside a Derived class, we have to use the keyword override along with the method declaration. The example is shown below.

In C#, a Base class reference can hold an object of the Derived class and when it invokes the methods, it will invoke always the Derived class methods, if you already override that method inside the Derived class by using the override keyword. Also the Base class method must declare using the keyword virtual. This is what is known as Polymorphism in C#.

// Inheritance : Virtual methods/Polymorphism

using System;
class Base
{
public virtual void Method()
{
Console.WriteLine("Base method");
}
}
class Derived : Base
{
public override void Method()
{
Console.WriteLine("Derived method");
}
}
class MyClient
{
public static void Main()
{
Base b1 = new Derived();
b1.Method(); // Displays 'Base Method'
}
}

As with a virtual method, we must include a method body with an override method; other wise the compiler will generate an error during compilation.

Remember that in C#, we can use the override keyword with only virtual method, when overriding inside a Derived class. An overridden declaration must be identical in every way to the virtual method, it overrides. They must have the same access level; the same return type, the same name and same method signature.

The overridden methods are implicitly virtual in nature and hence they can override in subsequent Derived classes. As like virtual methods, override methods can’t be declared as static or private.

No comments: