Tuesday, 13 May 2014

Difference Between virtual,override and new keyword

Simple Class Inheritance

Consider the below class hierarchy with classes A, B and C. A is the super/base class, B is derived from class A and C is derived from class B.
If a method Test() is declared in the base class A and classes B or C has no methods as shown below.
  1. using System;
  2. namespace Polymorphism
  3. {
  4. class A
  5. {
  6. public void Test() { Console.WriteLine("A::Test()"); }
  7. }
  8.  
  9. class B : A { }
  10.  
  11. class C : B { }
  12.  
  13. class Program
  14. {
  15. static void Main(string[] args)
  16. {
  17. A a = new A();
  18. a.Test(); // output --> "A::Test()"
  19.  
  20. B b = new B();
  21. b.Test(); // output --> "A::Test()"
  22.  
  23. C c = new C();
  24. c.Test(); // output --> "A::Test()"
  25.  
  26. Console.ReadKey();
  27.  
  28. }
  29. }
  30. }
Suppose you have Test() method in all the classes A, B, C as shown below:
  1. using System;
  2. namespace Polymorphism
  3. {
  4. class A
  5. {
  6. public void Test() { Console.WriteLine("A::Test()"); }
  7. }
  8.  
  9. class B : A
  10. {
  11. public void Test() { Console.WriteLine("B::Test()"); }
  12. }
  13.  
  14. class C : B
  15. {
  16. public void Test() { Console.WriteLine("C::Test()"); }
  17. }
  18.  
  19. class Program
  20. {
  21. static void Main(string[] args)
  22. {
  23. A a = new A();
  24. B b = new B();
  25. C c = new C();
  26.  
  27. a.Test(); // output --> "A::Test()"
  28. b.Test(); // output --> "B::Test()"
  29. c.Test(); // output --> "C::Test()"
  30.  
  31. a = new B();
  32. a.Test(); // output --> "A::Test()"
  33.  
  34. b = new C();
  35. b.Test(); // output --> "B::Test()"
  36.  
  37. Console.ReadKey();
  38. }
  39. }
  40. }
When you will run the above program, it will run successfully and gives the O/P. But this program will show the two warnings as shown below:
  1. 'Polymorphism.B.Test()' hides inherited member 'Polymorphism.A.Test()'. Use the new keyword if hiding was intended.
  2. 'Polymorphism.C.Test()' hides inherited member 'Polymorphism.B.Test()'. Use the new keyword if hiding was intended.

Method Hiding (new keyword)

As you have seen in the above example the compiler generate the warnings since C# also supports method hiding. For hiding the base class method from derived class simply declare the derived class method with new keyword. Hence above code can be re-written as :
  1. using System;
  2. namespace Polymorphism
  3. {
  4. class A
  5. {
  6. public void Test() { Console.WriteLine("A::Test()"); }
  7. }
  8.  
  9. class B : A
  10. {
  11. public new void Test() { Console.WriteLine("B::Test()"); }
  12. }
  13.  
  14. class C : B
  15. {
  16. public new void Test() { Console.WriteLine("C::Test()"); }
  17. }
  18.  
  19. class Program
  20. {
  21. static void Main(string[] args)
  22. {
  23. A a = new A();
  24. B b = new B();
  25. C c = new C();
  26.  
  27. a.Test(); // output --> "A::Test()"
  28. b.Test(); // output --> "B::Test()"
  29. c.Test(); // output --> "C::Test()"
  30.  
  31. a = new B();
  32. a.Test(); // output --> "A::Test()"
  33.  
  34. b = new C();
  35. b.Test(); // output --> "B::Test()"
  36.  
  37. Console.ReadKey();
  38. }
  39. }
  40. }
Moreover, if you are expecting the fourth and fifth output should be "B::Foo()" and "C::Foo()" since the objects a and b are referenced by the object of B and C respectively then you have to re-write the above code for Method Overriding.

Method Overriding (virtual and override keyword)

In C#, for overriding the base class method in derived class, you have to declare base class method as virtual and derived class method as override as shown below:
  1. using System;
  2. namespace Polymorphism
  3. {
  4. class A
  5. {
  6. public virtual void Test() { Console.WriteLine("A::Test()"); }
  7. }
  8.  
  9. class B : A
  10. {
  11. public override void Test() { Console.WriteLine("B::Test()"); }
  12. }
  13. class C : B
  14. {
  15. public override void Test() { Console.WriteLine("C::Test()"); }
  16. }
  17. class Program
  18. {
  19. static void Main(string[] args)
  20. {
  21. A a = new A();
  22. B b = new B();
  23. C c = new C();
  24. a.Test(); // output --> "A::Test()"
  25. b.Test(); // output --> "B::Test()"
  26. c.Test(); // output --> "C::Test()"
  27. a = new B();
  28. a.Test(); // output --> "B::Test()"
  29. b = new C();
  30. b.Test(); // output --> "C::Test()"
  31.  
  32. Console.ReadKey();
  33. }
  34. }
  35. }

Mixing Method Overriding and Method Hiding

You can also mix the method hiding and method overriding by using virtual and new keyword since the method of a derived class can be virtual and new at the same time. This is required when you want to further override the derived class method into next level as I am overriding Class B, Test() method in Class C as shown below:
  1. using System;
  2. namespace Polymorphism
  3. {
  4. class A
  5. {
  6. public void Test() { Console.WriteLine("A::Test()"); }
  7. }
  8.  
  9. class B : A
  10. {
  11. public new virtual void Test() { Console.WriteLine("B::Test()"); }
  12. }
  13.  
  14. class C : B
  15. {
  16. public override void Test() { Console.WriteLine("C::Test()"); }
  17. }
  18.  
  19. class Program
  20. {
  21. static void Main(string[] args)
  22. {
  23.  
  24. A a = new A();
  25. B b = new B();
  26. C c = new C();
  27.  
  28. a.Test(); // output --> "A::Test()"
  29. b.Test(); // output --> "B::Test()"
  30. c.Test(); // output --> "C::Test()"
  31.  
  32. a = new B();
  33. a.Test(); // output --> "A::Test()"
  34.  
  35. b = new C();
  36. b.Test(); // output --> "C::Test()"
  37.  
  38. Console.ReadKey();
  39. }
  40. }
  41. }

Note

  1. The virtual keyword is used to modify a method, property, indexer, or event declared in the base class and allow it to be overridden in the derived class.
  2. The override keyword is used to extend or modify a virtual/abstract method, property, indexer, or event of base class into derived class.
  3. The new keyword is used to hide a method, property, indexer, or event of base class into derived class.


-------------------------------------------------------------------------------------------------------

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 overridingdelegates example in c#sealed class in c#using statement in c#, OOPS examples in c#difference between wcf and web applicationinterview questions in asp.net, sql server, c# and many articles relating to interview questions in c#asp.netsql serverjavascriptjquery. Now I will explain difference between virtual override and new keywords inc#.net with example.

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

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

No comments:

Post a Comment