Overloading and Overriding
Overloading
The signature of a method is the name of the method; the number of, order of, and types of arguments of the method, as well as the return type.
The argument signature of a method is the number of, order of, and types of the arguments to a method.
A method is overloaded if it has the same name as another method but different argument signature. Overloading written in the same class itself with same method name with different arguments, may or may not be same return type written in the same class itself.
Overloading comes with in class for example :
There are Operator Overloading and Method overloading.
Method overloading
void Add( int i );
void Add( char c );
These two function declaration and definition in the same class is called method OVERLOADING.
Overriding
Overriding occurs only with respect to inheritance and occurs when a child has a method with the same signature as a parent's method.
Overriding comes with two class ( base and drived class ). Same method names with same arguments and same return types associated in a class and its subclass is called overriding.
example: function in the base class is like
public virtual void Display();
function in the drived class is like
public override void Display();
Now this function in the derived class overrides the base class .
Example for overriding
Clas A
{
Virtual void hi(int a)
{
}
}
Class B:A
{
public overrid void hi(int a)
{
}
}
Note the keyword Override to be used in C#.
An override method provides a new implementation of a member inherited from a base class. The method overridden by an override declaration is known as the overridden base method. The overridden base method must have the same signature as the override method. For information on inheritance, see Inheritance.
You cannot override a non-virtual or static method. The overridden base method must be virtual, abstract, or override.
An override declaration cannot change the accessibility of the virtual method. Both the override method and the virtual method must have the same access level modifier.
You cannot use the modifiers new, static, virtual, or abstract to modify an override method.
An overriding property declaration must specify the exact same access modifier, type, and name as the inherited property, and the overridden property must be virtual, abstract, or override.
Virtual Keyword
The virtual keyword is used to modify a method, property, indexer or event declaration, and allow it to be overridden in a derived class. For example, this method can be overridden by any class that inherits it: