Polymorphism in C#

Polymorphism refers to the phenomena where you have a single interface that can perform multiple functionalities. In C#, polymorphism is of two types: Static Polymorphism and Dynamic Polymorphism. Both of these types have been explained below.

Static Polymorphism

Static Polymorphism can be achieved by creating method with the same name but different number or types of parameters. This is best explained with the help of following example.

´╗┐using System;
namespace CSharpTutorials {

    public class Messages {

        public void DisplayType(int abc)  {
            Console.WriteLine("An int has been passed to this method");
        }

        public void DisplayType(string abc)  {

            Console.WriteLine("A string has been passed to this method");
        }

        public void DisplayType(char abc) {

            Console.WriteLine("A character has been passed to this method");
        }

        public void DisplayType(bool abc) {
            Console.WriteLine("A bool has been passed to this method");
        }
    }

    class Tutorial {
        static void Main(string[] args) {

            Messages msg = new Messages();
            msg.DisplayType(10);
            msg.DisplayType("hey");
            msg.DisplayType('c');
            msg.DisplayType(false);
            Console.ReadKey();
        }
    }
}

Download the code

In the above example, a class named Message has been defined. Inside this class four methods with the same name DisplayType have been declared. The only difference is the type of parameter that is passed to them. When these methods are called the method whose parameters match with those in the method call, will be executed. This is phenomena is known as method overloading and static overloading is achieved via this.

Dynamic Polymorphism

Dynamic Polymorphism is implemented via inheritance, abstract and virtual method. You can store object of the child class in the parent class variable and then if you call the method on that parent class object, it will call the child class method since it is storing reference of the object of the child class. This is explained in the following example.

´╗┐using System;

namespace CSharpTutorials {

    public class Vehicle {
        public virtual void DisplayMessage() {
            Console.WriteLine("This is a parent class method");
        }
    }

    public class Car : Vehicle {
        public override void DisplayMessage() {
            Console.WriteLine("This is an overridden child class method in Car");
        }
    }

    public class Truck : Vehicle {
        public override void DisplayMessage()    {
            Console.WriteLine("This is an overridden child class method in Truck");
        }
    }

    class Tutorial {
        static void Main(string[] args) {
            // Creating objects of car and truck class
            Vehicle v = new Vehicle();
            Vehicle car = new Car();
            Vehicle truck = new Truck();
            v.DisplayMessage();
            car.DisplayMessage();
            truck.DisplayMessage();
            Console.ReadKey();
        }
    }
}

Download the code

In the Main method, three vehicle type variables v, car and truck have been declared which stores references of Vehicle, Car and Truck objects. When DisplayMessage method is called on each object the implementation of the method intrinsic to each class will be executed which means you have single interface but different functionalities i.e. polymorphism.

<<< Interfaces in C#Virtual Methods in C# >>>
Copyright 2005-2016 KnowledgeHills. Privacy Policy. Contact .