OOPs Concept in C# - Polymorphism

Posted by Simplify DotNet on December 24, 2020 in , , | No comments

 Polymorphism


Polymorphism is one of the important concepts in OOPS, if you break the word into two parts then you will understand its meaning. Poly means many and morph means forms.

So we will be discussing how polymorphism is used in C#. There are basically two main types of polymorphism in C# as below.


  • Method Overloading.
  • Method Overriding.


So first we will understand method Overloading.

Method Overloading:

  In method overloading we have same method name for more than one method in a class but either the number of parameters for the method or data type of method are different. It is important to note that we can not have two methods with same method name, same number of parameters and there datatypes but different return type.

Let us understand the concept with an example.

So suppose if I have a class name as Test and in there I have two methods with the same name as add()
but if you look at those two methods then you will understand that both of them are accepting difference number of parameters.

public class Test 
{
        
      public int add(int a , int b)
     {
            return a + b;
     }    

     public int add(int a, int b, int c)
    {
            return a + b + c;
    }    


}

So when you build your code you will see that you have not encountered any errors even though both the methods are having same name, but if make the number of parameters and there data types same in both the method then you will get a compile time error.


Let us now discuss about method overriding.

Method Overriding:

    Method overriding means we can override implementation of method which is present in the parent class in child class. To override a method first we need to inherit from the parent class and the parent class needs to be a declared with virtual keyword.

    Let us understand this with an example so that you will get an clear idea. So we have a parent class as Parent and child class as Child and we have a method in the parent class named as foo() which is declared with keyword as virtual.

    public class Parent
    {
            public int a;
            public int b; 

            public virtual int foo(int a, int b)
            {    
                  //Implementation of parent foo method
            }
            
    }

    public class Child : Parent
    {
            public override int foo(int a, int a)
            {
                // implementation of child foo method.
            }                
    }


So as shown in the above example if you see we have overridden the implementation of foo() method in the child class and in the child class we can use the parent class data members a & b so it gives us code reusability also and child class can have its own functionality.

So if I need to give an example of method overriding in real life world then think of it as Car and different types of cars, so every car has a ability to move/drive but then each type of car like Swift, WagonR, i10 etc. will have its own type of implementation to get driven but every car have same data members like steering, wheels, gear etc...


So today, we have seen how we can use polymorphism in C#, we will discuss other OOPs concepts and many more things in my next post.


Thank You...…..

For any issue regarding C# or .NET please write to me on below mail id and I will try my best to reply.

Email Id : prson94@gmail.com.



0 comments:

Post a Comment