Polymorphism concept

Method Overloading
------------------

Method Overloading means having two or more methods with the same name but different signatures in the same scope. These two methods may exist in the same class or one in base class and another in the derived class.

The signature of a function is determined by 3 factors:

a) Number of arguments received by the function.
b) Data types of the parameters/arguments.
c) Position/order of the arguments.

The signature of a function never depends upon the return type. Two functions differ only in their return type cannot be overloaded.

Example of Method Overloading
-----------------------------




public partial class _Default : System.Web.UI.Page
{
    protected void Button1_Click(object sender, EventArgs e)
    {
        MyBaseClass mbc = new MyBaseClass();
        Response.Write(mbc.AddNumbers(1,2).ToString());
        Response.Write("<br />" + mbc.AddNumbers(1, 2, 3).ToString());
    }
}

public class MyBaseClass
{

    public int AddNumbers(int FirstNumber, int SecondNumber)
    {
        return FirstNumber + SecondNumber;
    }

    public int AddNumbers(int FirstNumber, int SecondNumber, int ThirdNumber)
    {
        return FirstNumber + SecondNumber + ThirdNumber;
    }
}


Method Overriding
-----------------

Method Overriding means having a different implementation of the same method in the inherited class. These two methods would have the same signature, but different implementation. One of these would exist in the base class and another in the derived class. These cannot exist in the same class.

Example of Overriding
---------------------

public partial class _Default : System.Web.UI.Page 
{
    protected void Button1_Click(object sender, EventArgs e)
    {
        MyBaseClass mbc = new MyBaseClass();
        Response.Write(mbc.Method1());
        MyBaseClass mdc = new MyDerivedClass();
        Response.Write("<br />" + mdc.Method1());
    }
}

public class MyBaseClass
{
    public virtual string Method1()
    {
        return "Base Class Method";
    }
}

public class MyDerivedClass : MyBaseClass
{
    public override string Method1()
    {
        return base.Method1() + "is called and in the Dereive Class Method, the base class method is reimplemented here.";
    }
}


Differences between Method Overloading and Method Overriding
------------------------------------------------------------

i) Overloading deals with multiple methods in the same class with the same name but different signatures. Where as, overriding deals with two methods, one in the base class and another in the child class, that have the same signature.
ii) Overloading lets you define a similar operation in different ways for different data. Overriding lets you define a similar operation in different ways of different object types.
iii) In Overloading methods should have different signatures. But, in Overriding methods should have same signatures.
iv) In Overloading, two or more methods may belong to the same class. But in Overriding, two methods must belong to two different classes.

Comments

Yamraj said…
Good explanation.....
Thank you....

Popular posts from this blog

sp_addmessage can be used to create a User-Defined Error Message

Command-line Building With csc.exe

Prepare DTO layer by using below script