C# Polymorphism


Polymorphism and Overriding Methods

Polymorphism means “many kinds”, and it happens when we have many categories related to each other by inheritance.

As we mentioned in the preceding chapter; Inheritance allows us to inherit fields and methods from another class.Polymorphism uses those methods to perform different functions. This allows us to perform the same action in different ways.

For example, think of a base class called Animal that has a method called animalSound(). Derived classes of Animals could be Pigs, Cats, Dogs, Birds - And they also have their own implementation of an animal sound (the pig oinks, and the cat meows, etc.):

Example
class Animal  // Base class (parent) 
            {
              public void animalSound() 
              {
                Console.WriteLine("The animal makes a sound");
              }
            }
            
            class Pig : Animal  // Derived class (child) 
            {
              public void animalSound() 
              {
                Console.WriteLine("The pig says: wee wee");
              }
            }
            
            class Dog : Animal  // Derived class (child) 
            {
              public void animalSound() 
              {
                Console.WriteLine("The dog says: bow wow");
              }
            }

We can now create Pig and Dog items and call the animalSound() in both:

Example
class Animal  // Base class (parent) 
            {
              public void animalSound() 
              {
                Console.WriteLine("The animal makes a sound");
              }
            }
            
            class Pig : Animal  // Derived class (child) 
            {
              public void animalSound() 
              {
                Console.WriteLine("The pig says: wee wee");
              }
            }
            
            class Dog : Animal  // Derived class (child) 
            {
              public void animalSound() 
              {
                Console.WriteLine("The dog says: bow wow");
              }
            }
            
            class Program 
            {
              static void Main(string[] args) 
              {
                Animal myAnimal = new Animal();  // Create a Animal object
                Animal myPig = new Pig();  // Create a Pig object
                Animal myDog = new Dog();  // Create a Dog object
            
                myAnimal.animalSound();
                myPig.animalSound();
                myDog.animalSound();
              }
            }
Example
class Animal  // Base class (parent) 
            {
              public virtual void animalSound() 
              {
                Console.WriteLine("The animal makes a sound");
              }
            }
            
            class Pig : Animal  // Derived class (child) 
            {
              public override void animalSound() 
              {
                Console.WriteLine("The pig says: wee wee");
              }
            }
            
            class Dog : Animal  // Derived class (child) 
            {
              public override void animalSound() 
              {
                Console.WriteLine("The dog says: bow wow");
              }
            }
            
            class Program 
            {
              static void Main(string[] args) 
              {
                Animal myAnimal = new Animal();  // Create a Animal object
                Animal myPig = new Pig();  // Create a Pig object
                Animal myDog = new Dog();  // Create a Dog object
            
                myAnimal.animalSound();
                myPig.animalSound();
                myDog.animalSound();
              }
            }