Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

C# - Difference between Virtual Override New Keywords with Example

Sep 23, 2013
Introduction:

Here I will explain difference between virtual override and new keywords in c#.net or virtual, override and new keywords example in c#.net

Description:

In previous posts I explained method overloading and overriding, delegates example in c#, sealed class in c#, using statement in c#, OOPS examples in c#, difference between wcf and web application, interview questions in asp.net, sql server, c# and many articles relating to interview questions in c#, asp.net, sql server, javascript, jquery. Now I will explain difference between virtual override and new keywords in c#.net with example.

Generally virtual and override keywords will occur in overriding method of polymorphism concept and new keyword will be used to hide the method. Here I will explain these keywords with example for that check below code.

I have one method Show() which exists in two classes SampleA and SampleB as shown below


using System;
namespace ConsoleApplication3
{
class SampleA
{
public void Show()
{
Console.WriteLine("Sample A Test Method");
}
}
class SampleB:SampleA
{
public void Show()
{
Console.WriteLine("Sample B Test Method");
}
}

class Program
{
static void Main(string[] args)
{
SampleA a=new SampleA();
SampleB b=new SampleB();
a.Show();
b.Show();
a = new SampleB();
a.Show();
Console.ReadLine();
}
}
}
When we run above program it will show output like as shown below but with some warning message in SampleB.Show() method like new keyword is required in ‘Show’ because it hides method in base class SampleA.Show()

Output


Sample A Test Method
Sample B Test Method
Sample A Test Method

New Keyword Example or Method Hiding

When we run above example it shows just warning message and display output this means that basically c# will support method hiding. To hide base class methods in derived classes without having any warning messages we can declare derived class methods with new keyword.  To use new keyword we need to write the code like as shown below


using System;
namespace ConsoleApplication3
{
class SampleA
{
public void Show()
{
Console.WriteLine("Sample A Test Method");
}
}
class SampleB:SampleA
{
public new void Show()
{
Console.WriteLine("Sample B Test Method");
}
}
class Program
{
static void Main(string[] args)
{
SampleA a=new SampleA();
SampleB b=new SampleB();
a.Show();
b.Show();
a = new SampleB();
a.Show();
Console.ReadLine();
}
}
}

Virtual and Override Keywords Example or Method Overriding

In method overriding we can override a method in base class by creating similar method in derived class this can be achieved by using inheritance principle and using “virtual & override” keywords. If we want to override base class method then we need to declare base class method with “virtual” keyword and the method which we created in derived class to override base class need to declare with “override” keyword like as shown below


using System;
namespace ConsoleApplication3
{
class SampleA
{
public virtual void Show()
{
Console.WriteLine("Sample A Test Method");
}
}
class SampleB:SampleA
{
public override void Show()
{
Console.WriteLine("Sample B Test Method");
}
}
class Program
{
static void Main(string[] args)
{
SampleA a=new SampleA();
SampleB b=new SampleB();
a.Show();
b.Show();
a = new SampleB();
a.Show();
Console.ReadLine();
}
}
}
When we run above program we will get output like as shown below

Output


Sample A Test Method
Sample B Test Method
Sample B Test Method

Use Both Method Overriding & Method Hiding

We can use both method hiding and method overriding by using virtual and new keyword at that time derived class method can be declared with virtual and new like as shown below


using System;
namespace ConsoleApplication3
{
class SampleA
{
public void Show()
{
Console.WriteLine("Sample A Test Method");
}
}
class SampleB:SampleA
{
public new virtual void Show()
{
Console.WriteLine("Sample B Test Method");
}
}
class SampleC : SampleB
{
public override void Show()
{
Console.WriteLine("Sample C Test Method");
}
}
class Program
{
static void Main(string[] args)
{
SampleA a=new SampleA();
SampleB b=new SampleB();
SampleB c = new SampleC();
a.Show();
b.Show();
c.Show();
a = new SampleB();
a.Show();
b = new SampleC();
b.Show();
Console.ReadLine();
}
}
}
Output


Sample A Test Method
Sample B Test Method
Sample C Test Method
Sample A Test Method
Sample C Test Method

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

4 comments :

Anonymous said...

nice article . but i thing last o/p is like,
Sample A Test Method
Sample B Test Method
Sample C Test Method
Sample B Test Method
Sample C Test Method

Amol Sonavane said...

nice ....now i got exact difference...thanx alot

Krishnat Ranage said...

Thanks a lot..!!!
Good article....If u add constructors and base keyword programmability it will better to understand overriding flow concept...and make helpful while facing interview.

Anonymous said...

Thig guy is awesome makes most complicated thing quite simple :) keep up the good work Suresh:)

Regards,
Praveen Nelge

Give your Valuable Comments

Other Related Posts

© 2010-2012 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.