At the time of overloading we generally use override with virtual. When we need to override the base class method into derived class than we use override keyword.
The difference between override and new is that override extend the method of base class with new definition but new hides the method of base class.
Before understanding the difference between both first we have to understand their use.
For Example
Create the following base class
public class baseClass
{
public string ShowHello()
{
return "Base Class hello";
}
}
Now create the derived class and inherit the base class into it
public class derClass:baseClass
{
public string ShowHello()
{
return "Derived Class hello";
}
}
As you can see in derived class it will not show any error but a following warning:-
Figure 1
If you assign new with this method it will stop giving warning. Use new in the following manner:-
public class derClass:baseClass
{
public new string ShowHello()
{
return "Derived Class hello";
}
}
Now create the object of derived class and call the method ShowHello()
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
derClass dr = new derClass();
Response.Write(dr.ShowHello());
}
}
You will see the following output:-
Figure 2
Override simply extends the functionality into derived class. The method which will be overridden in derived class will be assigned with virtual in base class.
For example:-
I have created the method in the base class and assign it with virtual in the following manner:-
public virtual string ShowWelcome()
{
return "Show Welcome from base";
}
Now I go to the derived class and override this method in the following manner:-
public override string ShowWelcome()
{
return "show welcome from derived";
}
Now call this method and checks its output:-
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
derClass dr = new derClass();
Response.Write(dr.ShowWelcome());
}
}
Figure 3
As its showing the method witch is overridden in derived class.
Now you are thinking that there is not any kind of difference between them till now as both are calling the derived class function.
Now create the object of derived class but use the type of base class in the following manner:-
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//take type of base class and initalize it with derived class object
baseClass bc = new derClass();
//concept of new assign here
Response.Write(bc.ShowHello()+"
");
//concept of override assign here
Response.Write(bc.ShowWelcome());
}
}
The output of the code as follows:-
Figure 4
As you can see that when I show method which used new keyword is showing base class method but when I show the method which used override then its shows the method of derived class.
As it shows the difference between new and override that override extends the method in derived class but new only hides the method of base class in derived class.