Friday, September 12, 2014

Polymorphism(Method Overloading and Overriding) in C#

Today's topic is a old one. Maximum among you are familiar with this topic. I am talking about one of the OOPs(Object Oriented Programming) feature polymorphism. In your college life you had come to know about polymorphism, but today I will discuss about this very known topic with the help of C#. As usual I have taken Find Area as an example like you did in your college.

Lets come to the point straight way.  Polymorphism has two parts.
  1. Method Overloading
  2. Method Overriding
Method Overloading :
So, lets start with the Method Overloading. Create a new project and take two textboxes, one button and one label for showing result.  Before starting one question came into the mind.. What actually method overloading is. Before this let me say about one very important issue about methods or function. A method name depends not only its name but also on its return type and argument. If you keep the name same and create a new method with a different return type and arguments it will act as a different method. This is  Method overloading. Calling the particular method, which has named more than one, but with distinct return type and argument.

In calculating area of a rectangle and a circle, the only difference is no of argument. In the first case you have to provide two argument(length and breath) but in the case of circle only one(radius) is enough to get the area. So, we will create two methods named FindArea() with 2 different argument. Lets check the following example.

/// 
/// For circle
/// 
/// 
/// 
private Double FindArea(string r)
{
     return 22 / 7 * Math.Pow(Convert.ToInt32(r),2);
}

/// 
/// For rectangle
/// 
/// 
/// 
/// 
private int FindArea(string l, string b)
{
     return (Convert.ToInt32(l) * Convert.ToInt32(b));
}        

Your methods are ready. Now you have to call these. For this generate click event of the button, and write down the following code.
protected void btnSubmit_Click(object sender, EventArgs e)
{
 /* For rectangle */
 if (txt1.Text != "" && txt2.Text != "")
 {
  lblArea.Text =  FindArea(txt1.Text.Trim(), 
   txt2.Text.Trim()).ToString();
 }
 /* for circle */
 else if (txt1.Text != "" && txt2.Text == "")
 {
  lblArea.Text = FindArea(txt1.Text.Trim()).ToString();
 }
 else
 {
  lblArea.Text = "Fill any of the two input box !";
 }
}

I hope it is clear to you what is method overloading and how its work. Now lets move to method overriding.

Method Overriding :

Not like method overloading its about inheritance. Before starting discussing about the inheritance is important. Inheritance is one of the property of OPPs programming concept, to get the all properties and methods of a parent class to the child class. Like all of your parents property is yours, because you are inheriting your parents.

So here we have taken two classes, parent.cs and child.cs and child.cs is inheriting parent.cs. So what ever variables or methods are declare in the parent.cs is accessible for child.cs.

We have declare abc() method in both class and creating  an object of child class. Using this object we are calling the abc() method. Now in the child class there are two abc() methods are present. One is its own and another is inherited from parent class. So, which method will be called? Answer is child class one. The child class method will override the parent class method to execute. Lets check the code once.
public class parent
{
 public parent() { }

 public string abc()
 {
  return "parent -> abc";
 }
}

public class child : parent
{
 public child() { }

 public string abc()
 {
  return "child -> abc";
 }
}

Now call the method.

child obj = new child();
Response.Write(obj.abc());

Output : child -> abc

Hope so both are clear for you, for your help you can download the full source code here.

0 comments:

Post a Comment

Follow by Email

Popular Posts

Pageviews