What is the difference between virtual, override and new keywords in c#?

Generally virtual and override keywords will occur in overriding method of polymorphism concept andnew 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 inSampleB.Show() method like new keyword is required in ‘Show’ because it hides method in base classSampleA.Show()

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();

}

}

}

 

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();

}

}

}

http://pramadha.com/

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s