CSharp OOP - Inheritance (Class inheritance, Interface implementation)

/*

Author: Jiangong SUN

*/


InheritancePolymorphism and Encapsulationare the three most important concepts in OOP.


Inheritance enables you to create new classes that reuse, extend, and modify the behavior that is defined in other classes. The class whose members are inherited is called the base class, and the class that inherits those members is called the derived class. 


A derived class can have only one direct base class. However, inheritance is transitive. 

A class can implement multiple instances.


I will show two examples of inheritance which are :

1) Class inheritance

2) Interface implementation



1) Class inheritance

A parent class with 2 constructors and 1 method:



A child class heritate the parent class and communicate with parent class.


Test them, the results are shown on the right of code.




2) Interface implementation


Here I've declared two interfaces with some methods signatures. 
PS: An interface contains only the signatures of methods, delegates or events, and of course properties.


Then a class implement these two interfaces with respective different implementation of every methods and properties declared. You should notice that, to distinguish the different implementations, the interface name should be ahead of the every single implementation of method if they have same names.

If they don't have same names, you just need to use "Public" ahead of the property or method, and you don't need to use interface as prefix.




Then the first 3 lines are to test the common implementations of methods.
Then the second and third parts are to test the method implementations for each interface.



I hope you can find this article helpful. Enjoy coding!


Reference:

http://csharp-station.com/Tutorial/CSharp/Lesson08

http://msdn.microsoft.com/en-us/library/64syzecx(v=vs.80).aspx


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值