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

11 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

Unknown said...

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

Unknown 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

Anonymous said...

Yes Article is awesome. but last output is wrong.. (:

Anonymous said...

the last o/p is correct. The reason is very simple. First three are straight forward. In the Fourth One, as new is mentioned, base class will take precedence, and the o/p will be A. In the last object instantiation, C overrides B. Hence, C

Anonymous said...

nopes, out is not correct

Anonymous said...

Output of last example is correct only. People should run and verify output before showing their intelligence publically and harming author's reputation.

Unknown said...

this is very helpful for fresher. inside this website everything has been explained very well.when i occur conceptual problem.i get A lot of solution from this web.
THANK YOU.

Anonymous said...

bad

rahul said...

its output same as as mentioned


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

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 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.