Polymorphism in .NET

In this article you will learn the following topics:

  1. Polymorphism
  2. Static or Compile-time Polymorphism
    •  Method overloading
  3. Dynamic or Runtime Polymorphism
    • Method Overriding
  4. Virtual modifier and Virtual methods
  5. Difference between method overriding and method hiding
  6. Sealed methods

What is Polymorphism?


Polymorphism means one name for many forms.

Polymorphism means one object behaving in multiple forms.

In other words, "Many forms of a single object is called Polymorphism."

One function behaves differently in multiple forms.

Real-World Example of Polymorphism


Example 1

A teacher behaves to student.
A teacher behaves to his/her seniors.
Here teacher is an object but the attitude is different in each situation.

Example 2

Person behaves son in the house at the same time that a person behaves an employee in an office.

Example 3

Your mobile phone, one name but many forms, such as:
  • As phone
  • As camera
  • As MP3 player
  • As radio

With polymorphism, the same method or property can perform different actions depending on the run-time type of the instance that invokes it.

There are two types of polymorphism:

  1. Static or Compile-time Polymorphism
  2. Dynamic or Runtime Polymorphism.
    Ploymorphism1.jpg

Static or Compile-time Polymorphism

  • In Static Polymorphism the decision is made at compile-time.
  • Which method is to be called is decided at compile-time only.
  • Method overloading is an example of this.
  • Compile-time polymorphism is method overloading, where the compiler knows which overloaded method it is going to call.

Method overloading is a concept where a class can have more than one method with the same name and different parameters.

The compiler checks the type and number of parameters passed to the method and decides which method to call at the compile-time and it will give an error if there are no methods that matches the method signature of the method that is called at the compile-time.

Example

  1. namespace MethodOverloadingByManishAgrahari  
  2. {  
  3.     class Program  
  4.     {  
  5.         public class TestOverloading  
  6.         {  
  7.             public void Add(string a1, string a2)  
  8.             {  
  9.                 Console.WriteLine("Adding Two String :" + a1 + a2);  
  10.             }  
  11.             public void Add(int a1, int a2)  
  12.             {  
  13.                 Console.WriteLine("Adding Two Integer :" +  a1 + a2);  
  14.             }  
  15.         }  
  16.         static void Main(string[] args)  
  17.         {  
  18.             TestOverloading obj = new TestOverloading();  
  19.             obj.Add("Manish " , "Agrahari");  
  20.             obj.Add(5, 10);  
  21.             Console.ReadLine();  
  22.         }  
  23.     }  
  24. } 

Ploymorphism2.jpg

Dynamic or Runtime Polymorphism


Run-time polymorphism is done by method overriding.

Method overriding allows us to have methods in the base and derived classes with the same name and the same parameters.

By runtime polymorphism we can point to any derived class from the object of the base class at runtime that shows the ability of runtime binding.

Through the reference variable of a base class, the determination of the method to be called is based on the object being referred to by a reference variable.

The compiler would not be aware of the availability of a method for overriding the functionality or not. So the compiler would not give any error at compile-time. At runtime it will be decided which method to call and if there is no method at runtime then it will give an error.

See the following example:
  1. namespace PolymorphismByManishAgrahari  
  2. {  
  3.     class Program  
  4.     {  
  5.         public class Base  
  6.         {  
  7.             public virtual void Show()  
  8.             {  
  9.                 Console.WriteLine("Show From Base Class.");  
  10.             }  
  11.         }  
  12.         public class Derived : Base  
  13.         {  
  14.             public override void Show()  
  15.             {  
  16.                 Console.WriteLine("Show From Derived Class.");  
  17.             }  
  18.         }  
  19.         static void Main(string[] args)  
  20.         {  
  21.             Base objBase;  
  22.             objBase = new Base();  
  23.             objBase.Show();//    Output ----> Show From Base Class.  
  24.             objBase = new Derived();  
  25.             objBase.Show();//Output--> Show From Derived Class.  
  26.             Console.ReadLine();  
  27.         }  
  28.     }  
  29. }   
The compiler demands the virtual "Show()" method and it compiles successfully. The right version of the Show() method cannot be determined until run-time since only at that time the base "objBase" is initialized as derived.

Virtual Modifier


According to MSDN, "The virtual keyword is used to modify a method, property, indexer or event declaration, and allow it to be overridden in a derived class.".

A virtual method is a method whose behavior can be overridden in a derived class.

Virtual method allows declare a method in base class that can be redefined in each derived class.

When a virtual method is invoked, the run-time type of the object is checked for an overriding member. The overriding member in the most derived class is called, that might be the original member, if no derived class has overridden the member.
  • By default, methods are non-virtual. You cannot override a non-virtual method.

  • You cannot use the virtual modifier with the static, abstract, private or override modifiers.

  • Virtual properties behave like abstract methods, except for the differences in declaration and invocation syntax.

  • It is an error to use the virtual modifier on a static property.

  • A virtual inherited property can be overridden in a derived class by including a property declaration that uses the override modifier.

Virtual method solves the following problem

In OOP when a derived class inherits from a base class, an object of the derived class may be referred to (or cast) as either being the base class type or the derived class type. If there are base class methods overridden by the derived class then the method call behavior is ambiguous.

In C#, polymorphism is explicit; you must have a virtual (or abstract) modifier on the base class method (member) and an override on the derived class method, that you probably already know.

If you don't put a modifier on a base class method, polymorphism can't ever happen. If you then add a non-modified method to the derived class with the same signature as the non-modified base class method then the compiler will generate a warning message.

See the following example:

  1. namespace PolymorphismByManishAgrahari  
  2. {  
  3.     class Program  
  4.     {  
  5.         public class Base  
  6.         {  
  7.             public virtual void Show()  
  8.             {  
  9.                 Console.WriteLine("Show From Base Class.");  
  10.             }  
  11.         }  
  12.         public class Derived : Base  
  13.         {  
  14.             //the keyword "override" change the base class method.  
  15.             public override void Show()  
  16.             {  
  17.                 Console.WriteLine("Show From Derived Class.");  
  18.             }  
  19.         }  
  20.         static void Main(string[] args)  
  21.         {  
  22.             Base objBaseRefToDerived  = new Derived();  
  23.             objBaseRefToDerived .Show();//Output--> Show From Derived Class.  
  24.             Console.ReadLine();  
  25.         }  
  26.     }  
  27. }  
It means that you are hiding (re-defining) the base class method. 

In other languages, Java for instance, you have what is called "implicit" polymorphism where just putting the method in the derived class with the same signature as a base class method will enable polymorphism. 

In Java, all methods of a class are virtual by default unless the developer decides to use the final keyword thus preventing subclasses from overriding that method. In contrast C# adopts the strategy used by C++ where the developer must use the virtual modifier for subclasses to override the method. Thus all methods in C# are non-virtual by default.

The C# approach is more explicit for the purpose of making the code safer in versioning scenarios. in other words you build your code based on a thirrd-party library and use meaningful, but common, method names. The thirrd-party library upgrades, using the same common method name. With implicit polymorphism the code would break, but with C# you would receive a compiler warning so you can double check to see if polymorphism was something you wanted to do.

Difference between Method Overriding and Method hiding


Method overriding allows a subclass to provide a specific implementation of a method that is already provided by the base class. The implementation in the subclass overrides (replaces) the implementation in the base class.
 
The important thing to remember about overriding is that the method that is doing the overriding is related to the method in the base class.

When a virtual method is called on a reference, the actual type of the object to which the reference refers determines which method implementation should be used. When a method of a base class is overridden in a derived class (subclass), the version defined in the derived class is used. This is so, even should the calling application be unaware that the object is an instance of the derived class.
  1. namespace PolymorphismByManishAgrahari  
  2. {  
  3.     class Program  
  4.     {  
  5.         public class Base  
  6.         {  
  7.             public virtual void Show()  
  8.             {  
  9.                 Console.WriteLine("Show From Base Class.");  
  10.             }  
  11.         }  
  12.         public class Derived : Base  
  13.         {  
  14.             //the keyword "override" change the base class method.  
  15.             public override void Show()  
  16.             {  
  17.                 Console.WriteLine("Show From Derived Class.");  
  18.             }  
  19.         }  
  20.         static void Main(string[] args)  
  21.         {  
  22.             Base objBaseRefToDerived  = new Derived();  
  23.             objBaseRefToDerived .Show();//Output--> Show From Derived Class.  
  24.             Console.ReadLine();  
  25.         }  
  26.     }  
  27. }  
Output
 
C# method overriding 

Method hiding does not have a relationship between the methods in the base class and derived class. The method in the derived class hides the method in the base class.

  1. namespace PolymorphismByManishAgrahari  
  2. {  
  3.     class Program  
  4.     {  
  5.         public class Base  
  6.         {  
  7.             public virtual void Show()  
  8.             {  
  9.                 Console.WriteLine("Show From Base Class.");  
  10.             }  
  11.         }  
  12.         public class Derived : Base  
  13.         {  
  14.             public new void Show()  
  15.             {  
  16.                 Console.WriteLine("Show From Derived Class.");  
  17.             }  
  18.         }  
  19.         static void Main(string[] args)  
  20.         {  
  21.             Base objBaseRefToDerived  = new Derived();  
  22.             objBaseRefToDerived .Show();//Output--> Show From Base Class.  
  23.             Console.ReadLine();  
  24.         }  
  25.     }  
  26. }  
Output is
 
C# Method hiding 

In the preceding example, Derived.Show will be called; because, it overrides Base.Show.

  • The C# language specification states that "You cannot override a non-virtual method.". See following example::

  1. namespace PolymorphismByManishAgrahari  
  2. {  
  3.     class Program  
  4.     {  
  5.         public class Base  
  6.         {  
  7.             public void Show()  
  8.             {  
  9.                 Console.WriteLine("This is Base Class.");  
  10.             }  
  11.         }  
  12.         public class Derived : Base  
  13.         {  
  14.             //Following Line will give error.  
  15.             /* 
  16.              Error:- 'PolymorphismByManishAgrahari.Program.Derived.Show()' 
  17.              cannot override inherited member  'PolymorphismByManishAgrahari.Program.Base.Show()' 
  18.              * because it is not marked virtual, abstract, or override 
  19.              */  
  20.             public override void Show()  
  21.             {  
  22.                 Console.WriteLine("This is Derived Class.");  
  23.             }  
  24.         }  
  25.         static void Main(string[] args)  
  26.         {  
  27.             Base objBase = new Base();  
  28.             objBase.Show();//    Output ----> This is Base Class.  
  29.             Derived objDerived = new Derived();  
  30.             objDerived.Show();//Output--> This is Derived Class.  
  31.             Base objBaseRefToDerived = new Derived();  
  32.             objBaseRefToDerived.Show();//Output--> This is Base Class.  
  33.             Console.ReadLine();  
  34.         }  
  35.     }  
  36. }  

Error: 'PolymorphismByManishAgrahari.Program.Derived.Show()' cannot override inherited member 'PolymorphismByManishAgrahari.Program.Base.Show()' because it is not marked virtual, abstract, or override

Sealed keyword


The Sealed keyword can be used to stop method overriding in derived classes.

By default, all methods are sealed, which means you can't override them, so that the "sealed" keyword is redundant in this case and the compiler will show you an error when you try to make sealed already sealed method. But if your method was marked as virtual in a base class, overriding and marking this method "sealed" will prevent method overriding in derived classes.

See the following example:
  1. namespace PolymorphismByManishAgrahari  
  2. {  
  3.     class Program  
  4.     {  
  5.         public class Base  
  6.         {  
  7.             public sealed void Show()//This Line will give an error - "cannot          
  8.             {     //be sealed because it is not an override"  
  9.                 Console.WriteLine("This is Base Class.");  
  10.             }  
  11.         }  
  12.         public class Derived : Base  
  13.         {  
  14.             public void Show()  
  15.             {  
  16.                 Console.WriteLine("This is Derived Class.");  
  17.             }  
  18.         }  
  19.         static void Main(string[] args)  
  20.         {  
  21.             Base objBaseReference = new Derived();  
  22.             objBaseReference.Show();// Output ---------> This is Base Class.  
  23.             Console.ReadLine();  
  24.         }  
  25.     }  
  26. }  
Error

'PolymorphismByManishAgrahari.Program.Base.Show()' cannot be sealed because it is not an override

To remove the error from the program above use the following:

  1. namespace PolymorphismByManishAgrahari  
  2. {  
  3.     class Program  
  4.     {  
  5.         public class Base  
  6.         {  
  7.             public virtual void Show()  
  8.             {  
  9.                 Console.WriteLine("This is Base Class.");  
  10.             }  
  11.         }  
  12.         public class Derived : Base  
  13.         {  
  14.             public override sealed void Show()  
  15.             {  
  16.                 Console.WriteLine("This is Derived Class.");  
  17.             }  
  18.         }  
  19.         static void Main(string[] args)  
  20.         {  
  21.             Base objBaseReference = new Derived();  
  22.             objBaseReference.Show();// Output ---> This is Derived Class.  
  23.             Console.ReadLine();  
  24.         }  
  25.     }  
  26. }  
Output
 
C# Polymorphism 

Summary

  1. It is not compulsory to mark a derived/child class function with the override keyword when the base/parent class contains a virtual method

  2. Virtual methods allow subclasses to provide their own implementation of that method using the override keyword

  3. Virtual methods can't be declared as private.

  4. You are not required to declare a method as virtual. But, if you don't, and you derive from the class, and your derived class has a method by the same name and signature, you'll get a warning that you are hiding a parent's method

  5. A virtual property or method has an implementation in the base class, and can be overridden in the derived classes.

  6. We will get a warning if we won't use the Virtual/New keyword.

  7. Instead of virtual we can use the new operator

References


http://en.wikipedia.org/wiki/Virtual_function

 


Recommended Free Ebook
Similar Articles