Polymorphism in C#

Polymorphism in C# :



Polymorphism is one of the fundamental concepts of OOP.
It allows you to invoke methods of derived class through base class reference during runtime.It has the ability for classes to provide different implementations of methods that are called through the same name.

Types of Polymorphism:



There are 2 types of Polymorphism namely
1.Compile time polymorphism (or) Overloading
2.Runtime polymorphism (or) Overriding

Compile Time Polymorphism :



Compile time polymorphism is method and operators overloading. It is also called early binding.
Method with same name but with different arguments is called method overloading.
In method overloading, method performs the different task at the different input parameters.


public class A1
{
public void hello()
{ Console.WriteLine(“Hello"); }

public void hello(string s)
{ Console.WriteLine(“Hello {0}",s); }
}



Runtime Time Polymorphism :



Runtime time polymorphism is done using inheritance and virtual functions. Method overriding is called runtime polymorphism. It is also called late binding.
Method overriding occurs when child class declares a method that has the same type arguments as a method declared by one of its superclass.
When overriding a method, you change the behavior of the method for the derived class. Overloading a method simply involves having another method with the same prototype.
Method overloading has nothing to do with inheritance or virtual methods.


public Class parent
{
virtual void hello()
{ Console.WriteLine(“Hello from Parent"); }
}

public Class child : parent
{
override void hello()
{ Console.WriteLine(“Hello from Child"); }
}

public static void main()
{
parent objParent = new child();
objParent.hello();
}


Comments

Author: Ramesh Sahu17 Aug 2009 Member Level: Gold   Points : 0

Well Described Polymorphism functionalites..
Good.



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: