This Angular tutorial helps you get started with Angular quickly and effectively through many practical examples.

RunTime Polymorphism


In this article we are going to discuss Run-Time Polymorphism in C#. There are many blogs, articles are available on the internet regarding Run-Time Polymorphism but in this particular article I will try to explain to you with as much as simplest and realistic examples so you can get a clear idea of the Run-Time Polymorphism use in C#.

RunTime Polymorphism (Dynamic Polymorphism)

In C# programming, the Method Overriding is also called Run time polymorphism, Late binding, or Dynamic Polymorphism. Run-time polymorphism is achieved through method overriding. This type of polymorphism is resolved during runtime.

This type of polymorphism is resolved during RunTme . RunTime polymorphism is achieved through:

  1. Method overriding

1. Method Overriding

Method overriding allows a subclass to provide a specific implementation of a method that is already defined in its superclass. we can also say Overriding is used to modify and provide a new implementation of the method inherited from a base class. Some Important points related to Method Overriding:

  1. Multiple methods of same name in different class.
  2. Inheritance is used, as it is in different class.
  3. All methods have same signature.
  4. It’s a run time polymorphism.
  5. Virtual & override keywords.
RunTime PolyMorphism

Example

Let us see an example for understanding Method Overriding in C#. Please have a look at the below code. Here class Animal is the parent class and in this class, we defined one method i.e. MakeSound() by using the virtual keyword which marks this method to be overridable inside the child classes. The class Dog is derived from the class Animal and hence it becomes a child class of class Animal and as soon as it becomes a child class, it got permission to override the overridable method MakeSound(). As you can see in the child class, we override the MakeSound() method by using the override modifier.

public class Animal
{
    // Virtual method
    public virtual void MakeSound()
    {
        Console.WriteLine("Some generic animal sound");
    }
}

public class Dog : Animal
{
    // Overriding the base class method
    public override void MakeSound()
    {
        Console.WriteLine("Bark");
    }
}

public class Cat : Animal
{
    // Overriding the base class method
    public override void MakeSound()
    {
        Console.WriteLine("Meow");
    }
}

// Usage
class Program
{
    static void Main()
    {
        Animal myDog = new Dog();
        Animal myCat = new Cat();

        myDog.MakeSound(); 
        myCat.MakeSound();
    }
}

Output

RunTime PolyMorphism

When do we need to override a method in C#?

If the Super Class or Parent Class method logic is not fulfilling the Sub Class or Child Class business requirements, then the Sub Class or Child Class needs to override the superclass method with the required business logic. Usually, in most real-time applications, the Parent Class methods are implemented with generic logic which is common for all the next-level sub-classes.

Overriding the Virtual Method is Optional in C#?

The point that you need to keep in mind is overriding the virtual method in the child classes is optional. If you are not overriding the virtual method means you going with the default implementation which is provided by the superclass.


public class Animal
{
    // Virtual method
    public virtual void MakeSound()
    {
        Console.WriteLine("Some generic animal sound");
    }
}

public class Dog : Animal
{
    // Overriding the base class method
    
}


// Usage
class Program
{
    static void Main()
    {
        Animal myDog = new Dog();
        myDog.MakeSound(); 
      
    }
}

Output

RunTime PolyMorphism

Now, let us understand the Main method code. Please observe the following code first. In this case, the type of the reference variable and the object the variable points to are the same i.e. Dog

Animal myDog = new Dog();
myDog.MakeSound();  

So, at the time of compilation, the compiler will check the Show method inside Dog and it will not find the Show method inside this class. So, it will again go and check the Superclass of Dog which is Animal and it finds the method inside the Animal and it will link that method definition from Animal with the method call.

How can we execute the superclass method if it is overridden in the sub-class in C#?

By creating the parent class object under the child class, we can call the parent class methods from the child class, or by using the base keyword, we can call parent class methods from the child class, but this and the base keyword cannot be used under the static block.

Example

Let us see an example for a better understanding. As you can see in the below code, from the child class Show method we are calling the parent class Show method by using base.Show() method call.

public class Animal
{
    // Virtual method
    public virtual void MakeSound()
    {
        Console.WriteLine("Some generic animal sound");
    }
}

public class Dog : Animal
{
    // Overriding the base class method
    
    public override void MakeSound()
    {
        base.MakeSound(); // base keyword
        Console.WriteLine("Bark");
    }
}

// Usage
class Program
{
    static void Main()
    {
        Animal myDog = new Dog();
        Dog myDog1 = new Dog();

        myDog.MakeSound(); 
        myDog1.MakeSound(); 
    }
}

Output

RunTime PolyMorphism

How to Implement Method Hiding/Shadowing in C#?

It is used to hide both virtual and non-virtual methods under the child class by using the new keyword. It does not matter whether the parent class method is virtual or not.

In Method Hiding, you can completely hide the implementation of the methods of a base class from the derived class using the new keyword.

Example


public class Animal
{
    // Virtual method
    public virtual void MakeSound()
    {
        Console.WriteLine("Some generic animal sound");
    }
}

public class Dog : Animal
{
    //Method Hiding/Shadowing
    public new  void MakeSound()
    {
        Console.WriteLine("Bark");
    }
}

// Usage
class Program
{
    static void Main()
    {
        Animal myDog = new Dog();
        myDog.MakeSound(); 
    }
}

Output

RunTime PolyMorphism
Next