Polymorphisam in c#

What is polymorphisam?

This is the one of important concept of c#. in an interview it's the most common question which could be asked.

Polymorphism is a Greek word, meaning "one name many forms". In other words, one object has many forms or has one name with multiple functionalities. "Poly" means many and "morph" means forms. Polymorphism provides the ability to a class to have multiple implementations with the same name. It is one of the core principles of Object Oriented Programming.

Types of polymorphisam:

There are two types of polymorphisam:
  1. Static/Compile time polymorphisam - Method overloading and Operator overloading is a type compile time polymorphisam
  2. Dynamic/Run time polymorphisam. - Method overriding is a type of run time polymorphisam.

Static or Compile Time Polymorphism

  • It is also known as Early Binding. 
  • Method overloading is an example of Static Polymorphism. 
  • In overloading, the method / function has a same name but different signatures. It is also known as Compile Time Polymorphism because the decision of which method is to be called is made at compile time. 
  • Overloading is the concept in which method names are the same with a different set of parameters.

In below example you can see that there are two methods with same name Overloading_Example class.

 Example:

 class Program
    {
        static void Main(string[] args)
        {
            Overloading_Example obj = new Overloading_Example();
            int add2 = obj.Add(45, 34, 67);
            int add1 = obj.Add(23, 34);
        }
    }

    public class Overloading_Example
    {
        public int Add(int a, int b, int c)
        {
            return a + b + c;
        }
        public int Add(int a, int b)
        {
            return a + b;
        }
    }


Dynamic / Runtime Polymorphism

  • Dynamic / runtime polymorphism is also known as late binding. 
  • the method name and the method signature (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 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 method to call at runtime and if no method is found then it throws an error.

Example:

public class Animal
    {
        public virtual void eat()
        {
            Console.WriteLine("Eating...");
        }
    }
    public class Dog : Animal
    {
        public override void eat()
        {
            Console.WriteLine("Eating bread...");
        }
    }
    public class TestOverriding
    {
        public static void Main()
        {
            Dog d = new Dog();
            d.eat();
            Console.ReadLine();
        }
    }





No comments:

Post a Comment

Please let me know if you have any feedback or doubts.

Jagged Array

What is Jagged Array? Jagged array is called as "array of arrays". when you need a data inside your array element at that time you...