CSharp OOP - Polymorphism (Compile Time, Run Time, Parametric)

/*

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();


And then, I want to test them.



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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值