Runtime polymorphism

Runtime polymorphism in C# is executed via virtual methods. In this type of polymorphism, the compiler executes the code by identifying its form at runtime. 

In the Method overriding section, we learned about virtual methods and saw how they allow the derived class to override the implementation of a function in the base class. In runtime polymorphism, the object of the base class holds the reference to objects of the base and derived classes. Now, based on the object that the base object is pointing to, the appropriate function will be loaded.

To recap our understanding of this, let's go through another code example. In this example, we will create a base class called Animal that will be inherited by two classes, Man and Dog

The following is the implementation in the Animal class:

public class Animal
{
public int numOfHands;
public int numOfLegs;
public virtual void Speak()
{
Console.WriteLine("This is a base implementation in the base animal class");
}
}

In the Animal class, we have declared two attributes to represent numOfHands and numOfLegs of the Animal. We have also declared a function called Speak and have marked it as Virtual so that any class that inherits from this class can give its own implementation of the Speak functionality.

We have declared the Speak function as virtual, which means that this function can be overridden in the derived class.

The following is the implementation in the Dog class: 

public class Dog : Animal
{
public string breed;
public Dog(string breed, int hands, int legs)
{
this.breed = breed;
base.numOfHands = hands;
base.numOfLegs = legs;
}

public override void Speak()
{
Console.WriteLine("A dog will bark , its breed is " + this.breed + " and number of legs and hands are " + this.numOfLegs + " " + this.numOfHands);
}
}

In this implementation, we have created a Dog class that is inheriting from the Animal class. The Dog class has an attribute called Breed and a constructor that takes three parameters of breed, hands, and legs, respectively. We also have a Speak function to provide an impression of how a dog object will implement the Speak functionality.

The following code is for another class, Human, which will also inherit from the base class of Animal:

public class Human : Animal
{
public string countryOfCitizenship;
public Human(string citizenship, int hands, int legs)
{
this.countryOfCitizenship = citizenship;
base.numOfHands = hands;
base.numOfLegs = legs;
}
public override void Speak()
{
Console.WriteLine("A man can speak multiple languages, its citizenship is " + this.countryOfCitizenship + " and number of legs and hands are " + this.numOfLegs + " " + this.numOfHands);
}
}

In the preceding code, we are doing the following:

  • We are inheriting the Dog class from the base class of Animal.
  • We are overriding the Speak function in the derived class.
  • We are also using the attributes that were declared in the base class.

Now, let's see how runtime polymorphism works. In the following code, we are declaring an object of the base Animal class and pointing it to an object of the derived class:

Animal animal = new Animal();
animal.numOfHands = 2;
animal.numOfLegs = 4;
animal.Speak();

animal = new Dog("Labrador", 0, 4);
animal.Speak();

animal = new Human("India", 2, 2);
animal.Speak();
Console.ReadLine();

Once we execute this code, we will notice that, based on the class object reference that the base object animal is pointing to, the appropriate implementation of the Speak method will be loaded. This loading is decided at runtime, which is why this is called runtime polymorphism:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.247.181