Definition: Method signature in base class as well as in derived class should be same.
If you are not satisfied with the implementation of a method in the parent class, you can keep the same declaration (signature and return type), but provide a different implementation.
Important Points::
- By using virtual and override keywords we can accomplish Method overriding.
- The virtual modifier indicates to derived classes that they can override this method.
- The override modifier allows a method to override the virtual method of its base class at run-time.
To achieve Run-time polymorphism or Late Binding, we are using Method Overriding concept.
Here, I am writing a simple program.
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication3
{
class baseclass
{
public virtual int Add(int a, int b)
{
return a + b;
}
}
class subclass : baseclass
{
public override int Add(int x, int y)
{
int d = 20;
return base.Add(x,y)+d;

Suresh PaldiaPosted Oct 28, 2010, 6:57 AM
Thank You Jitendra for valuable information. But i have a doubt. What if i don't use a virtual and override keywords and still write same signatured method in base and derived class? At runtime if i create an object of derived class, wil it give the same results?