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. |
|||
|
|||
11 comments :
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
nice ....now i got exact difference...thanx alot
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.
Thig guy is awesome makes most complicated thing quite simple :) keep up the good work Suresh:)
Regards,
Praveen Nelge
Yes Article is awesome. but last output is wrong.. (:
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
nopes, out is not correct
Output of last example is correct only. People should run and verify output before showing their intelligence publically and harming author's reputation.
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.
bad
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
Note: Only a member of this blog may post a comment.