Polymorphism
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:
Class Student inherits from class Person. Method info() is overridden.
- public class Person
- {
- protected string name;
- public Person()
- {
- }
- public Person(string n)
- {
- name = n;
- }
- virtual public void info() // Polymorphism = late binding -> keyword 'virtual'
- //public void info() // Type conversion at compile time
- {
- Console.WriteLine("Name of person: " + name);
- }
- public string Name
- {
- get
- {
- return name;
- }
- }
- }
- public class Student : Person
- {
- public Student()
- {
- }
- public Student(string n)
- {
- name = n;
- }
- override public void info() // Polymorphism = late binding -> 'override'
- //new public void info() // works only with early binding (binding at compile time)
- {
- Console.WriteLine("Name of student: " + name);
- }
- }
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:
- class Program
- {
- static void Main(string[] args)
- {
- // At first: NO POLYMORPHISM, early binding -> the call of the respective method of Person/Student
- // was already defined at compile time.
- // This works with "new" as well as "virtual" and "override"
- Person p = new Person("p");
- Student s = new Student("s");
- p.info();
- s.info();
- // This only works with "new"
- Person p2 = (Person)s;
- p2.info();
-
Student s2 = (Student)p;// This would throw an InvalidTypeCastException at runtime - // because person is a subset of student and not vice versa
- // This is POLYMORPHISM, late binding -> The decision to call the respective method of Person/Student
- // is made at runtime.
- // only works with "virtual" and "override"
- List<Person> persons = new List<Person>();
- char ch;
- do
- {
- string name = "";
- Console.Write("Create person -> p\tstudent -> s? ");
- ch = Console.ReadKey().KeyChar;
- if(ch == 'p' || ch == 's')
- {
- Console.Write("\tName: ");
- name = Console.ReadLine();
- }
- if(ch == 'p')
- persons.Add(new Person(name));
- if(ch == 's')
- persons.Add(new Student(name));
- }while(ch == 'p' || ch == 's');
- foreach (Person ps in persons)
- {
- Console.WriteLine();
- ps.info();
- if (ps is Person)
- Console.WriteLine("I am a person");
- if (ps is Student)
- Console.WriteLine("I am a student");
- }
- Console.ReadKey();
- }
- }

// 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