/*
Author: Jiangong SUN
*/
Today I will clarify the concepts of polymorphism in CSharp with some code examples.
Generally, CSharp has three ways of polymorphisms: Compile time polymorphism, Run time polymorphism and parametric polymorphism
1) Compile time polymorphism (means "method overloading": methods with same name, but different number or different types of parameters)
Here I've created a base class and a derived class for demonstrating method overloading. As you can see, there is an "Addition" impementation with a integer parameter in base class. And there are some "Addition" implementations with different number of parameters or different types of parameters.
You should know the following code won't work, because the derived class have extended the base class.
DerivedCompileTimePolymorphism dctp = new CompileTimePolymorphism();
2) Run time polymorphism (means "method overriding": methods with same name, same number and same types of parameters)
Now I create a base class with two "Addition" methods and a class constructor. For the second method, "virtual" keyword means that the method could be overridden by its child class.
And then, I've created a child class to inherate the base class.
The new modifier instructs the compiler to use your implementation instead of the base class implementation. Any code that is not referencing your class but the base class will use the base class implementation.
The override modifier may be used on virtual methods and must be used on abstract methods. This indicates for the compiler to use the last defined implementation of a method. Even if the method is called on a reference to the base class it will use the implementation overriding it.
"new" and "virtual" can work together for the reason that : user will use a combination of some methods in parent class and some methods in child class.
For example:
BaseClass instance = new ChildClass();
In this way, the instance of BaseClass will get its original implementation, but not overridden implementation of its child class.
Another child class who will override the base virtual method.
The first instance "drt" will apparently use methods in "DerivedRunTimePolymorphism".
The second instance "rt" will use methods of "RunTimePolymorphismBase". Because "new" keyword is used to implement the same methods in child class, not in base class. And "new virtual" keyword means that child method will not override base method.
The third instance "drt2" is just a instance of "DerivedRunTimePolymorphism2".
The fourth instance "rt2" will use its own first normal method and the method overridden by its child class "DerivedRunTimePolymorphism2".
3) parametric polymorphism
Parametric Polymorphism is a well-established programming language feature. Generics offers this feature to C#.
Finally, we are at the end of this article. I hope it does help to you! Enjoy coding!
reference:
http://www.codeproject.com/Articles/6207/Generics-C
http://rosettacode.org/wiki/Parametric_polymorphism#C.23
http://stackoverflow.com/questions/5825106/design-issue-in-c-generics-or-polymorphism-or-both