Close
Close

Polymorphism in C#


Polymorphism means the ability to take different forms. In C#, there are two types of polymorphism - compile-time polymorphism and run time polymorphism.

Compile time polymorphism is also known as early binding or late binding. Runtime polymorphism is also known as dynamic binding or late binding.

In C#, we can achieve compile time polymorphism with method overloading and runtime polymorphism can be achieved by method overriding which we are going to study in this chapter.

C# Method Overloading


Same as constructors, we can also overload methods. Method overloading is basically having multiple methods with same name but different arguments. Suppose we want to make a method to add numbers. It is possible that some sometimes we need to add two integer, sometimes two doubles, etc. We can use method overloading to have multiple methods with same name e.g. - Add(int x, int y), Add(int x, int y, int z), Add(double x, double y), Add(int x, double y), etc.

Conditions for method overloading are:-

  1. Methods to be overloaded must have the same name.
  2. All methods must have different arguments (either a different number of parameters or different type of parameters).

Let's take an example.

using System;

class Rectangle
{
public static void PrintArea(int x, int y)
{
  Console.WriteLine(x*y);
}
public static void PrintArea(int x)
{
  Console.WriteLine(x*x);
}
public static void PrintArea(int x, double y)
{
  Console.WriteLine(x*y);
}
public static void PrintArea(double x)
{
  Console.WriteLine(x*x);
}
static void Main(string[] args)
{
  PrintArea(2, 4);
  PrintArea(2, 5.1);
  PrintArea(10);
  PrintArea(2.3);
}
}
Output
8
10.2
100
5.29

Here, we defined four methods with the same name PrintArea but different parameters.

Firstly the method PrintArea is called and 2 and 4 passed to it. Since both 2 and 4 are integers, so the method named PrintArea with both its parameters of type int (int x, int y) is called.

After that, method is called with 2 and 5.1. Since 2 is of type int and 5.1 is of type double, so the method with the first parameter of type int and the second one of type double i.e., (int x,double y) is called.

Similarly, after that the function with only one integer value as its parameter is called and at last, the function with a single double value as its parameter is called.

method overloading in C#

C# Method Overriding


Method overriding is also like method overloading but it performed with parent and child class. Suppose, we have made an Animal class and there is a method Speak in the class which prints "Animal Speach". Now, we have made a subclass Dog of the class Animal and we want the method Speak to print "Bark" in this method.

We can override the Speak method of the Animal class in the Dog subclass to print "Bark".

We use virtual keyword with the method which we are going to override (in parent class) and override keyword with the method in the child class. Let's look at an example.

using System;

class Animal
{
public virtual void Sound()
{
  Console.WriteLine("This is parent class");
}
}

class Dog: Animal
{
public override void Sound()
{
  Console.WriteLine("Dogs bark");
}
}

class Test
{
static void Main(string[] args)
{
  Dog d = new Dog();
  d.Sound();
}
}
Output
Dogs bark

In the above example, the class Dog and its parent class Animal have the same method void sound(). When the object d of class Dog calls this method, then the method of the child class Dog is called, not that of the parent class. Thus, the method of the child class overrides the method in parent class when called by an object of the child class.

C# Sealed Method


To prevent a method from being overridden, we use sealed keyword. Let's look at an example.

using System;

class A
{
public virtual void AMethod()
{
  Console.WriteLine("This is parent class");
}
}

class B:A
{
public sealed override void AMethod()
{
  Console.WriteLine("This is parent class");
}
}

class C:B
{
public override void AMethod()
{
  Console.WriteLine("This is parent class");
}
}

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

}
}
Output
hello.cs(21,24): error CS0239: 'C.AMethod()': cannot override inherited member 'B.AMethod()' because it is sealed

In the class B, we have sealed the method AMethod. So, we are not able to override it in the class C.


Ask Yours
Post Yours
Doubt? Ask question