• Register
100 points
4

Overview 

Today we know about polymorphism in C # in our website kodlogs. What is this polymorphism and what does it facilitate us in C #. Let's read it in detail.

Polymorphism means

Polymorphism capacity "many forms", and it takes place when we have many instructions that are associated to every different through inheritance.Like we distinct in the preceding chapter; Inheritance lets us inherit fields and techniques from any other class. Polymorphism makes use of these strategies to function extraordinary tasks. This approves us to function a single motion in one of a kind ways.

Example 

For example, assume of a base classification known as Animal that has a technique referred to as animalSound(). Derived training of Animals ought to be Pigs, Cats, Dogs, Birds - And they additionally have their very own implementation of an animal sound۔

class Animal  
{
  public virtual void animalSound() 
  {
    Console.WriteLine("The animal makes a sound");
  }
}

class Pig : Animal  
{
  public override void animalSound() 
  {
    Console.WriteLine("The pig says: wee wee");
  }
}

class Dog : Animal  
{
  public override void animalSound() 
  {
    Console.WriteLine("The dog says: bow wow");
  }
}

class Program 
{
  static void Main(string[] args) 
  {
    Animal myAnimal = new Animal();  
    Animal myPig = new Pig(); 
    Animal myDog = new Dog();  

    myAnimal.animalSound();
    myPig.animalSound();
    myDog.animalSound();
  }

                 OUTPUT will be.
The animal  makes a sound
The pig says: wee wee wee
The dog says: bow bow bow

I hope you will understand polymorphism in C#

100 points
4