.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> C# >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Polymorphism

Posted By:Syed Shakeer Hussain       Posted Date: March 09, 2009    Points: 25    Category: C#    URL: http://www.dotnetspark.com  
 

Generally, it is the ability to appear in different forms. In oops concept, it is the ability to process objects differently depending on their data types. Its the ability to redefine methods for derived classes.

 
There Are Two Types of Polymorphism

  1. Static Polymorphism
  2. Dynamic Polymorphism
1. Static Polymorphism
In Static Polymorphism ,Which method is to be called is decided at compile-time only. Method Overloading is an example of Static Polymorphism.

Method overloading is a concept where we use the same method name many times in the same class,but different parameters. Depending on the parameters we pass, it is decided at compile-time only, which method is to calles. The same method name with the same parameters is an error and it is a case of duplication of methods which c# does not permits. In Static Polymorphism decision is taken at compile time.

public Class StaticPolyDemo
{
  public void display(int x)
 {
    Console.WriteLine("Area of a Square:"+x*x);
 }
  public void display(int x, int y)
 {
    Console.WriteLine("Area of a Square:"+x*y);
 }
 public static void main(String args[])
 {
    StaticPolyDemo spd=new StaticPolyDemo();
    Spd.display(5);
   Spd.display(10,3);
 }
}

2. Dyanamic Polymorphism

In this Mechanism by which a call to an overridden function is resolved at a Run-Time( not at Compile-time). If a BaseClass contains a method that is overridden.


Class Test
{
  Public void show()
 {  
  Console.WriteLine("From base class show method");
    }
}
Public Class DynamicPolyDemo : Test
{
   Public void show()
   {
         Console.WriteLine("From Derived Class show method");
   }
   Public static void main(String args[])
   {
       DynamicPolyDemo dpd=new DynamicPolyDemo();
      Dpd.show();
       
    }
}
 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend