Method Overriding in C#
Using the keywords virtual , override and Base we can extend or override the logic of the methods derived in Base class. But the keyword new keyword we can hide the method derived in the base class.
Let's see the use of each keyword in detail.
using System;
namespace Method_overriding
{
class Override
{
public virtual void show()
{
Console.WriteLine("Baseclass Show");
}
}
class override2:Override
{
public override void show()
{
base.show();Calling base class method
Console.WriteLine("Sub show");
}
}
class Program
{
static void Main(string[] args)
{
Override over = new Override();
over.show();
Console.ReadLine();
}
}
}
namespace Method_overriding
{
class Override
{
public virtual void show()
{
Console.WriteLine("Baseclass Show");
}
}
class override2:Override
{
public override void show()
{
base.show();
Console.WriteLine("Sub show");
}
}
class Program
{
static void Main(string[] args)
{
Override over = new Override();
over.show();
Console.ReadLine();
}
}
}
Output
Baseclass Show
Explanation
using System;
namespace Method_overriding
{
class Override
{
public virtual void show()
{
Console.WriteLine("Baseclass Show");
}
}
class override2:Override
{
public override void show()
{
base.show();Calling base class method
Console.WriteLine("Sub show");
}
}
class Program
{
static void Main(string[] args)
{
Override over = new Override2();
over.show();
Console.ReadLine();
}
}
}
namespace Method_overriding
{
class Override
{
public virtual void show()
{
Console.WriteLine("Baseclass Show");
}
}
class override2:Override
{
public override void show()
{
base.show();
Console.WriteLine("Sub show");
}
}
class Program
{
static void Main(string[] args)
{
Override over = new Override2();
over.show();
Console.ReadLine();
}
}
}
Output
Baseclass Show
Sub show
Sub show
Explanation
Now we will remove the Base.show() from the subclass show() method.
using System;
namespace Method_overriding
{
class Override
{
public virtual void show()
{
Console.WriteLine("Baseclass Show");
}
}
class override2:Override
{
public override void show()
{
base.show() removed
Console.WriteLine("Sub show");
}
}
class Program
{
static void Main(string[] args)
{
Override over = new Override2();
over.show();
Console.ReadLine();
}
}
}
namespace Method_overriding
{
class Override
{
public virtual void show()
{
Console.WriteLine("Baseclass Show");
}
}
class override2:Override
{
public override void show()
{
Console.WriteLine("Sub show");
}
}
class Program
{
static void Main(string[] args)
{
Override over = new Override2();
over.show();
Console.ReadLine();
}
}
}
In the above program , note we have created object of parent class but given the reference of subclass.
Output
Sub show
Use of new keyword
As you see above , with the virtual, override keywords we override the parent class method with subclass method. But if you see , if we created object of parent class and gave reference of subclass , then the subclass method will be called not base class method.
But in case of new keyword, there is slight difference. If you create object of Parent class and gave the reference of subclass , then the parent class method will be called, because the new keyword hides the implementation of base class method in derived class. see the below example.
using System;
namespace Method_overriding
{
class Override
{
public virtual void show()
{
Console.WriteLine("Baseclass Show");
}
}
class override2:Override
{
public new void show()
{
Console.WriteLine("Sub show");
}
}
class Program
{
static void Main(string[] args)
{
Override over = new Override2();
over.show();
Console.ReadLine();
}
}
}
namespace Method_overriding
{
class Override
{
public virtual void show()
{
Console.WriteLine("Baseclass Show");
}
}
class override2:Override
{
public new void show()
{
Console.WriteLine("Sub show");
}
}
class Program
{
static void Main(string[] args)
{
Override over = new Override2();
over.show();
Console.ReadLine();
}
}
}
Output
Baseclass Show
Please comment below if you have any questions.
Method Overriding in C#
Reviewed by LanguageExpert
on
April 27, 2018
Rating:
No comments