Understanding Polymorphism In C#

Introduction

Polymorphism is a key feature of OOP that lets developers use same name methods in different forms. This tutorial explains the basics of Polymorphism and how to implement polymorphism in C#. 

Polymorphism in C#

Polymorphism is a Greek word meaning "one name many forms." "Poly" means many, and "morph" means forms. In other words, one object has many forms or has one name with multiple functionalities. Polymorphism allows a class to have multiple implementations with the same name. It is one of the core principles of Object Oriented Programming after encapsulation and inheritance. In this article, you'll learn what polymorphism is, how it works, and how to implement it in C#.

Types of Polymorphism

There are two types of polymorphism in C#:

  • Static / Compile Time Polymorphism
  • Dynamic / Runtime Polymorphism

polymorphism

Static or compile-time polymorphism 

Method overloading is an example of Static polymorphism. TOverloading is the concept in which method names are the same with different parameters. The method/function has the same name but different signatures in overloading. It is also known as Early binding. It is also known as compile-time polymorphism because the decision of which method is to be called is made at compile time.

Here C# compiler checks the number of parameters passed, and the parameter type decides which method to call and throws an error if no matching method is found.

In the following example, a class has two methods with the same name, "Add," but with different input parameters ("the first method has three parameters and the second method has two parameters)

public class TestData  
{  
    public int Add(int a, int b, int c)  
    {  
        return a + b + c;  
    }  
    public int Add(int a, int b)  
    {  
        return a + b;  
    }  
}  
class Program  
{  
    static void Main(string[] args)  
    {  
        TestData dataClass = new TestData();  
        int add2 = dataClass.Add(45, 34, 67);  
        int add1 = dataClass.Add(23, 34);  
    }  
}  

static polymorphism

Dynamic / Runtime Polymorphism

Dynamic/runtime polymorphism is also known as late binding. Here, the method name and the method signature (the number of parameters and parameter type must be the same and may have a different implementation). Method overriding is an example of dynamic polymorphism.

Method overriding can be done using inheritance. With method overriding, it is possible for the base class and derived class to have the same method name and the same something. The compiler would not be aware of the method available for overriding the functionality, so the compiler does not throw an error at compile time. The compiler will decide which way to call at runtime, and if no method is found, it throws an error.

public class Drawing  
{  
    public virtual double Area()  
   {  
         return 0;  
   }  
}  
  
public class Circle : Drawing  
{  
    public double Radius { get; set; }  
    public Circle()  
    {  
        Radius = 5;  
    }  
    public override double Area()  
    {  
        return (3.14) * Math.Pow(Radius, 2);  
    }  
}  
  
public class Square : Drawing  
{  
    public double Length { get; set; }  
    public Square()  
    {  
        Length = 6;  
    }  
    public override double Area()  
    {  
        return Math.Pow(Length, 2);  
    }  
}  
  
public class Rectangle : Drawing  
{  
    public double Height { get; set; }  
    public double Width { get; set; }  
    public Rectangle()  
    {  
        Height = 5.3;  
        Width = 3.4;  
    }  
    public override double Area()  
    {  
        return Height * Width;  
    }  
}  
  
class Program  
{  
    static void Main(string[] args)  
    {  
  
        Drawing circle = new Circle();  
        Console.WriteLine("Area :" + circle.Area());  
  
        Drawing square = new Square();  
        Console.WriteLine("Area :" + square.Area());  
  
        Drawing rectangle = new Rectangle();  
        Console.WriteLine("Area :" + rectangle.Area());  
    }  
}  

dynamic polymorhism

The compiler requires an Area() method, and it compiles successfully, but the right version of the Area() method is not being determined at compile time but determined at runtime. Finally, the overriding methods must have the same name and signature (number of parameters and type) as the virtual or abstract method defined in the base class method and that it is overriding in the derived class.

A method or function of the base class is available to the child (derived) class without using the "overriding" keyword. The compiler hides the"function o" method of the base class. This concept is known as shadowing or method hiding. You may find the difference between overriding and shadowing here.

Preventing Derived class from overriding virtual members

Virtual members remain “virtual” indefinitely. In other words, virtual members remain “virtual” regardless of how many classes have been b"tween them and the class initially declared it. For example, if class X has the virtual method "A," class Y is derived from X, and class Z "s "is derived from Y, class Z inherits the virtual method "A" and overrides it.

public class X  
{  
    public virtual void A()  
    {  
    }  
}  
public class Y : X  
{  
    public override void A()  
    {  
    }  
}  

A derived class can stop virtual inheritance by declaring an overriding member as "sealed"

public class Y : X  
{  
    public sealed override void A()  
    {  
    }  
}  

Accessing Base class virtual member

Using the "he "base" keyword, the derived class can access"the "Method.

public class X  
{  
    public virtual void A()  
    {  
    }  
}  
public class Y : X  
{  
    public override void A()  
    {  
        base.A();  
    }  
}  

Summary

The meaning of polymorphism is one name has multiple forms.

The following are the two types of polymorphism:

  1. Static or compile-time polymorphism (method overloading and operator overloading).
  2. Dynamic or runtime polymorphism (for example, overriding).

Other points about polymorphism:

  • Method Overriding differs from shadowing.
  • Using the "new" keyword, we can hide the base class member."
  • We can prevent a derived class from overriding virtual members.
  • We can access a base class virtual member from the derived class.

I hope this helps!

Polymorphism is one of the key pillars of OOPs. Check out what other pillars make OOP one of the top programming languages here, Object Oriented Programming In C# (c-sharpcorner.com)

Method overloading is a way to implement polymorphism in C#. Here is a detailed article on Method Overloading In C#.


Similar Articles