Polymorphism in C# with example

Polymorphism is derived from two Latin words, 1. Poly means many 2. Marphose means forms. By using inheritance, a class can be used as many types, say we have a Person which may be a Professor, may be a Student, may be Clerk. So in this article we will see some example of Polymorphism.

When we derive a class from base class it inherits all the method, properties, fields and events of the base class. To change the behavior or data of base class we have two ways, either we can hide the base method by using new key word or override the base method by using override key word.

Let's create a class Person which we will use as base class for other classes, note we used virtual for Display method so we can override it in our derived classes.

public class Person
{
    public virtual void Display()
    {
        Console.WriteLine("I am a person!");
    }
}

Now create some other classes derived from Person class and override display method in every class.

public class Professor: Person
{
    public override void Display()
    {
        Console.WriteLine("I am a professor!");
    }
}

public class Student : Person
{
    public override void Display()
    {
        Console.WriteLine("I am a student!");
    }
}

public class Clerk : Person
{
    public override void Display()
    {
        Console.WriteLine("I am a clerk!");
    }
}

Now we will create an object of person and initialize with different classes one by one and call their display method

Person person = new Person();
person.Display();

person = new Professor();
person.Display();    

person = new Student();
person.Display();

person = new Clerk();
person.Display();

What would be the out put, any guess, do you think it will display 4 times "I am a person!", now, here is the out put

I am a person!
I am a professor!
I am a student!
I am a clerk!

So in this example we see person is behaving like a person, professor, student and then as a clerk. This is the power of polymorphism, a single object behaves in different forms.

As we know, to hide a parent method we use new key word so let's change our student class and don't override but use new key word see this

public class Student : Person
{
    public new void Display()
    {
        Console.WriteLine("I am a student!");
    }
}

Our other classes are unchanged, so let's check the output of following

Person person = new Person();
person.Display();

person = new Professor();
person.Display();    

person = new Student();
person.Display();

person = new Clerk();
person.Display();
// Output
I am a person!
I am a professor!
I am a person!*  
I am a clerk!

* Person don't know anything about the display method of student because it hides it. only when we create an object of student and initialize with student then it's display method will show I am a student.

For overloading and overriding see my other article Overloading and Overriding in C# with example

Hamden Process manager with a reputed organization, Fond of learning new features and technology related to C#, ASP.Net, SQL Server, MVC etc.I like to help others, if I can
  • c#
  • polymorphism
  • oops
By Hamden On 01 Jun, 13  Viewed: 4,564

Other blogs you may like

How to create windows service in C# visual studio 2010

Creating windows service in C# is really easy but when we need to create first time we face many problems so in this post we will try to learn everything about window service. In this article we will create a windows service which will run after a particular interval, and time should be... By Ali Adravi   On 30 Apr 2013  Viewed: 8,641

Covariance and Contravariance in C# 4.0 with example

C# the language, right from the very beginning is always supported covariance and contravariance but in most simple scenario, now in C# 4.0 we have full support of covariance and contravariance in all circumstances, generic interface and generic delegates, other C# types already support from first... By Ali Adravi   On 29 Mar 2013  Viewed: 4,988

default value for different data type in C#

In C# there are different data type and they use some default value when we declare a variable. When we define a variable of type int or Int32 say int score; so what is the value of score, will it be null or zero, in the same way if we create a variable of type string/String what value it holds... By Ali Adravi   On 27 Mar 2013  Viewed: 2,615

enum in C# and conversion

An enumeration type (also known an enumeration or an enum) provides an efficient way to define a set of named integral constants that may be assigned to a variable to make programming clear, understandable and manageable. There are ways of using, like how to use enum in switch and case statement,... By Hamden   On 27 Mar 2013  Viewed: 4,571

Dynamic vs var in C# with example

When we have var which dynamically holds any type or value so why we need dynamic type, what are the differences and situations where we can use dynamic rather than var, there were some question in my mind so I explored more which I want to share here. There are two things, static type and... By Nathan Armour   On 26 Mar 2013  Viewed: 3,219