Friday 30 August 2013

How to Implementing Polymorphism is C#.Net

What is Polymorphism?


Polymorphism means one name, many forms .In other words polymorphism can be explained as one entity having multiple forms. Polymorphism is important feature of OOPS.By using this feature of polymorphism, you can create multiple methods having same name. For Example, suppose you have to write program that calculates the area of triangle, circle, rectangle, square .With the Polymorphism feature, you can create four different methods with same name Area for each. The number and types of parameters and return types of methods may be different. It provides various advantages which are follows:


  1. Help classes to provide different implementations for method that are called using same name
  2. Help you to call a method of a class regardless of the specific implementations of the method
  3. Allow you to invoke method of a derived class through  base class reference at run time in C#, polymorphism is following two types:


1.    Compile –Time Polymorphism
2.    Run-Time Polymorphism

Compile -Time Polymorphism:

When a compiler compiles a program, the compiler has information about the method arguments; accordingly, the compiler binds the appropriate method to the respective object at the compile time itself. This process is called compile-time polymorphism or early binding. You can implement its through overloaded methods and operators. The arguments passed to methods are matched in terms of number, type and order; and the overloaded methods are invoked.

Compile-Time Polymorphism can be implemented by using two methods:


  1.  Method Overloading
  2. 2 Operator Overloading

Method Overloading: When the methods defined in a class behave according to number and types of parameters passed to them they are called overloaded methods and this behavior of the methods is called method overloading .when you overload a method, then the overloaded version of the method that is called by a method call at run time is determined by the signature with which the method is called. A method signature comprises the following attributes:


  1.    The name of the method is same
  2.     The number of the parameters passed to the method
  3.     The data type of the parameters
  4.      The order in which the parameters are passed


Implementing Method Overloading with Example:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Polymorphism
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

public class Shape
{
// create method Area with one Parameter for Calculate Area of Square
public void Area(double side)
{
MessageBox.Show("Area of Square is=" + (side * side));
}
// create method Area with two Parameters for Calculate Area of Rectangle
public void Area(double length,double width)
{
MessageBox.Show("Area of Rectangle is=" + (length * width));
}
}

private void button1_Click(object sender, EventArgs e)
{
Shape sp = new Shape();
//concept of Method Overloading
sp.Area(Convert.ToDouble(txtside.Text));
sp.Area(Convert.ToDouble(txtlength.Text), Convert.ToDouble(txtwidth.Text));

}
}
}





                                                                                                               Operator Overloading

0 comments:

Post a Comment