Polymorphism (from greek for diversity, complex shape) is a concept in object-oriented programming that allows an identifier of object to accept objects of different classes (same inheritance hierachy) at runtime.

The most common use of polymorphism in OOP occurs when a superclass reference is used to refer to a subclass object. At runtime, the system decides whether to call the method of the superclass or the overwritten method of the subclass:


Student inherits from Person. Method info() is overridden.Class Student inherits from class Person. Method info() is overridden.



  1. public class Person
  2. {
  3.     protected string name;
  4.     public Person()
  5.     {
  6.     }
  7.     public Person(string n)
  8.     {
  9.         name = n;
  10.     }
  11.  
  12.     virtual public void info()      // Polymorphism = late binding -> keyword 'virtual'
  13.     //public void info()            // Type conversion at compile time
  14.     {
  15.         Console.WriteLine("Name of person: " + name);
  16.     }
  17.     public string Name
  18.     {
  19.         get
  20.         {
  21.             return name;
  22.         }
  23.     }
  24. }
  25. public class Student : Person
  26. {
  27.     public Student()
  28.     {
  29.     }
  30.     public Student(string n)
  31.     {
  32.         name = n;
  33.     }
  34.     override public void info() // Polymorphism = late binding -> 'override'
  35.     //new public void info()    // works only with early binding (binding at compile time)
  36.     {
  37.         Console.WriteLine("Name of student: " + name);
  38.     }
  39.         
  40. }


Now an identifier of type of superclass can take both types of object, an object of class Person or an object of class Student. But not vice versa!

When the assignment of this object happens at runtime and not at compile time, so-called polymorphism causes the call the method of the superclass or the overwritten method of the subclass:


  1. class Program
  2. {
  3.     static void Main(string[] args)
  4.     {
  5.            
  6.         // At first: NO POLYMORPHISM, early binding -> the call of the respective method of Person/Student
  7.         // was already defined at compile time.
  8.  
  9.  
  10.         // This works with "new" as well as "virtual" and "override"
  11.         Person p = new Person("p");
  12.         Student s = new Student("s");
  13.         p.info();
  14.         s.info();
  15.  
  16.         // This only works with "new"
  17.         Person p2 = (Person)s;
  18.         p2.info();
  19.  
  20.         Student s2 = (Student)p;    // This would throw an InvalidTypeCastException at runtime
  21.                                     // because person is a subset of student and not vice versa
  22.  
  23.             
  24.             
  25.         // This is POLYMORPHISM, late binding -> The decision to call the respective method of Person/Student
  26.         // is made at runtime.
  27.  
  28.         // only works with "virtual" and "override"
  29.         List<Person> persons = new List<Person>();
  30.         char ch;
  31.         do
  32.         {
  33.             string name = "";
  34.             Console.Write("Create person -> p\tstudent -> s? ");
  35.             ch = Console.ReadKey().KeyChar;
  36.             if(ch == 'p' || ch == 's')
  37.             {
  38.                 Console.Write("\tName: ");
  39.                 name = Console.ReadLine();
  40.             }
  41.             if(ch == 'p')
  42.                 persons.Add(new Person(name));
  43.             if(ch == 's')
  44.                 persons.Add(new Student(name));
  45.         }while(ch == 'p' || ch == 's');
  46.         foreach (Person ps in persons)
  47.         {
  48.             Console.WriteLine();
  49.             ps.info();
  50.             if (ps is Person)
  51.                 Console.WriteLine("I am a person");
  52.             if (ps is Student)
  53.                 Console.WriteLine("I am a student");
  54.         }        
  55.         Console.ReadKey();
  56.     }
  57. }


Screenshot of program polymorphism

// early binding -> NO POLYMORPHISM

// late binding -> POLYMORPHISM 1. creation of a person
// 2. creation of a student
// quit

// the person is an instance of Person

// the student is both - a person and a student, which makes sense


Output of the program

Last modified: Wednesday, 23 October 2019, 9:32 AM