Polymorphism is the concept used to identify the observation that objects change behavior according to condition.
Etymology: Poly - many, Morph - forms (Greek)
There are two types of polymorphism - static (or compile time) and dynamic (or runtime polymorphism).
Static polymorphism / Compile time polymorphism
Method overloading is an example of static polymorphism or compile time polymorphism.
In method overloading, same method exists with multiple signatures.
The return type has no role to play in differentiating the different method signatures.
Which method will be invoked is decided at the compile time or during the build.
Operators can also be overloaded. Operator overloading is also an example of static polymorphism.
Dynamic polymorphism / Runtime polymorphism
Inheritance makes method overriding possible. This is the example of dynamic or runtime polymorphism.
Keywords such as virtual, override, new etc. are used to implement overriding and method hiding in C#.
Variable of the type base class can point to any object of the derived class. However vice versa is not possible.
So object reference of the base class type can point to object of the derived class.
If there are two different methods of same name in two different classes derived from the same base class, the object of the base type will call the method in either of the derived classes depending on the class whose object it points to.
Also a method in the derived class may override the method in the base class of the same name. Whether the base class method or the derived class method is invoked, is decided at runtime based on what class object the variable points to.
This is dynamic or runtime polymorphism.
Etymology: Poly - many, Morph - forms (Greek)
There are two types of polymorphism - static (or compile time) and dynamic (or runtime polymorphism).
Static polymorphism / Compile time polymorphism
Method overloading is an example of static polymorphism or compile time polymorphism.
In method overloading, same method exists with multiple signatures.
The return type has no role to play in differentiating the different method signatures.
Which method will be invoked is decided at the compile time or during the build.
Operators can also be overloaded. Operator overloading is also an example of static polymorphism.
Dynamic polymorphism / Runtime polymorphism
Inheritance makes method overriding possible. This is the example of dynamic or runtime polymorphism.
Keywords such as virtual, override, new etc. are used to implement overriding and method hiding in C#.
Variable of the type base class can point to any object of the derived class. However vice versa is not possible.
So object reference of the base class type can point to object of the derived class.
If there are two different methods of same name in two different classes derived from the same base class, the object of the base type will call the method in either of the derived classes depending on the class whose object it points to.
Also a method in the derived class may override the method in the base class of the same name. Whether the base class method or the derived class method is invoked, is decided at runtime based on what class object the variable points to.
This is dynamic or runtime polymorphism.
No comments:
Post a Comment