Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




C# - POLYMORPHISM


Polymorphism by definition means many forms. Which signifies, an object can exist in different forms.


Story :

An Animal exist in different forms. They are Dog, Cat, Deer e.t.c. So an Animal is not a physical entity but Cat, Dog etc are. So, the Animal creator god had prepared an abstract class called Animal. But why an abstract class?

Because we know, all animals breathes oxygen. So, the breathe() behaviour can be specified. But what about the sound() behaviour? Cats and Dogs don't make the same sound. So, sound() is abstract. Thus the Animal class is abstract.

Animal class


abstract class Animal : LivingBeing
{
	public void breathe()
	{
		System.Console.WriteLine("Breathes Oxygen.");
	}

	public abstract void sound(); //Undefined so abstract
}

Story :

So, he obeyed the rule made by the Master God. He defined the breathe() behaviour but couldn't define the sound() behaviour and made it abstract, that's absolutely ok.

Thus, he made this class abstract and made this rule that all Animals should breathe oxygen but the sound they makes will be determined by the gods who creates actual animals (i.e Cat, Dog etc).

Now, the Cat creator God, Dog creator God e.t.c came into picture.

Cat Class


public class Cat : Animal
{
	public override void sound()
	{
		System.Console.WriteLine("Cats Meao");
	}

	public void catRelated()
	{
		System.Console.WriteLine("It loves sleeping.");
	}
}

And for Dog :


Dog Class


public class Dog : Animal
{
	public override void sound()
	{
		System.Console.WriteLine("Dog Barks");
	}

	public void dogRelated()
	{
		System.Console.WriteLine("Very obidient.");
	}
}

Thus, we have the below hierarchy :

C_Sharp

So we have seen how Animal exists in various forms. Since Animal is abstract, we cannot create any object out of it. Also if you think in reality Animal does not exist. But Cat, Dog etc exists and thus we can create Cat or Dog objects.


Now, comes the most vital part. Creating Cat and Dog objects using the Animal reference. Also known as Dynamic Binding. Little tricky but used everywhere.


Example :



abstract class Animal
{
	public void breathe()
	{
		System.Console.WriteLine("Breathes Oxygen.");
	}

	public abstract void sound(); //Undefined so abstract
}

class Cat : Animal
{
	public override void sound()
	{
		System.Console.WriteLine("Cats Meao");
	}

	public void catRelated()
	{
		System.Console.WriteLine("It loves sleeping.");
	}
}

class Dog : Animal
{
	public override void sound()
	{
		System.Console.WriteLine("Dog Barks");
	}

	public void dogRelated()
	{
		System.Console.WriteLine("Very obidient.");
	}
}

class CreateLivingBeing
{
	public static void Main(string[] args)
	{
    	Animal animalCat = new Cat();
    	Animal animalDog = new Dog();
    	animalCat.sound(); // Calls the sound() behaviour of Cat.
    	animalDog.sound(); // Calls the sound() behaviour of Dog.	
    	animalCat.breathe(); // Calls the breathe() behaviour of Animal.
    	animalDog.breathe(); // Calls the breathe() behaviour of Animal.
	}
}


Output :



  Cats Meao
  Dog Barks
  Breathes Oxygen.
  Breathes Oxygen.

In the above code, we have seen something new. The Animal reference variable animalCat is holding a Cat object.


Animal animalCat = new Cat();
Animal animalDog = new Dog();

Story :

Although it looks strange for a while, but think logically, the Animal creator God has created the Animal class. And did set the rules which Cat/Dog creator God has to obey. Being a master of Cat/Dog creator he should have the access to their objects as well. Sounds ok?

Now, lets dig the below line:


Animal animalCat = new Cat();

Here Animal is the Refrence Type, animalCat is the refrence variable and new Cat() is the Instance/object.