Theme-Logo
  •   .Net
    •   C Sharp(C#)
    •   Web API
    •   Micro Services
    •   ASP.Net
    • ASP.Net MVC
    • .Net Core
  •   Database
    • SQL Server
    • Oracle
    • PostgreSQL
  •   jQuery
    • jQuery Tutorials
    • jQuery Plugins
    • jQuery UI
    • More on jquery
  •   Tutorials
    • Microservices Tutorials
    • DotNet Core Tutorials
    • PostgreSql Tutorials

Polymorphism in Object-Oriented Programming(OOPS)

Polymorphism is one of the fundamental concepts of OOP. The word polymorphism means having many forms. In simple words, we can define polymorphism as the ability of a message to be displayed in more than one form.

Real life example of polymorphism

A person at the same time can have different characteristic. Like a man at the same time is a father, a husband, an employee.

What is Polymorphism?

Polymorphism is a way in which we can define multiple functions in a single name i.e. single name and multiple meaning.

In below example(DP example) we have a we have same method name with different parameters:

                                        void CalculateArea(int side);  
                                        void CalculateArea(int l, int b);  
                                        void CalculateArea(float radius);  
                                

Types of Polymorphism in C#

Polymorphism is of two types. They are:

  • 1. Compile time polymorphism/Overloading
  • 2. Runtime polymorphism/Overriding
  • 1. Compile time polymorphism

    Compile time polymorphism is method and operators overloading. It is also called early binding. Below is example of method overloading:

                                   class Program  
                                            {  
                                                public class WebsiteInfo  
                                                {  
                                                    public void display(string name)  
                                                    {  
                                                         Console.WriteLine("Website name is :" + name);  
                                                    }  
                                                    public void display(int rank, string name)  
                                                    {  
                                                        Console.WriteLine("Website name is :" + name);  
                                                        Console.WriteLine("Ranking of website is : " + rank);  
    
                                                    }  
                                                }  
                                                static void Main(string[] args)  
                                                {  
                                                    WebsiteInfo obj = new WebsiteInfo();  
                                                    obj.display("DotNat Palace");  
                                                    obj.display(210000, "dotnetpalace.com");  
                                                    Console.ReadLine();  
                                                }  
                                            } 
                                    

    You should keep in mind that overloads must be different in their signature, which means the name and the number and type of parameters. You should consider overloading a method when you for some reason need a couple of methods that take different parameters, but conceptually do the same thing.

    2. Runtime polymorphism

    Runtime time polymorphism is done using inheritance and virtual functions. Method overriding is called runtime polymorphism. It is also called late binding.

    When overriding a function, you change the behavior of the function for the derived class

    Following is the example of implementing a run time polymorphism in C#.

                                     public class Users
                                        {
                                            public virtual void GetUserInfo()
                                            {
                                                Console.WriteLine("Base Class User");
                                            }
                                        }
                                        // Derived Class
                                        public class Details : Users
                                        {
                                            public override void GetUserInfo()
                                            {
                                                Console.WriteLine("Derived Class User");
                                            }
                                        }
                                    class Program
                                        {
                                            static void Main(string[] args)
                                            {
                                                Users d = new Users();
                                                d.GetUserInfo();
                                                Details b = new Details();
                                                b.GetUserInfo();
                                                Console.WriteLine("\nPress Enter Key to Exit..");
                                                Console.ReadLine();
    
                                            }
    
                                        }
                                    

    Summary

    To summarise, The polymorphism is the feature of object-oriented programming which indicates single name with multiple meaing. You should use polymorphism for almost same reason you need couple (or more than that) of method that take different parameters (eg. Calculate area of different types of shapes). Hope you picked up a thing or two from this article.

    Cheers!

    ❮ Inheritance
    encapsulation ❯

    Trending Post
    Dependency Injection in C#
    Inversion of controls (IoC)
    Design patterns
    Service-Oriented Architecture(SOA)
    Interview Questions and Answers
    What is Request delegate?
    What is Host in ASP.NET Core?
    Describe the Generic Host and Web Host?
    Describe the Servers in ASP.NET Core?
    How to read values from Appsettings.json file?
    How to handle errors in ASP.NET Core?
    About us

    DotNet Palace is a community platform created and maintained by The articles are mainly focused on Microsoft stack technologies like C#, ASP.Net, MVC, .Net Core, SQL Server and Oracle, PostgreSQL, SQLite etc. To improve the site's content you can send your valuable suggestions at info.dotnetpalace@gmail.com

    Quick links
  • SOLID Principles
  • Questions
  • OOPS Principles
  • DotNet Palace Tutorials
  • Privacy Policy
  • Terms and Condition