C# - Recommendations for Abstract Classes vs. Interfaces
The choice of whether to design your functionality as an interface or an abstract class can sometimes be a difficult one. An
abstract class is a class that cannot be instantiated, but must be inherited from. An abstract class may be fully implemented, but is more usually partially implemented or not implemented at all, thereby encapsulating common functionality for inherited
classes.
An interface, by contrast, is a totally abstract set of members that can be thought of as defining a contract for conduct. The implementation of an interface is left completely to the developer.
Both interfaces and abstract classes are useful for component interaction. If a method requires an interface as an argument, then any object that implements that interface can be used in the argument. Abstract
classes also allow for this kind of polymorphism, but with a few caveats:
- Classes may inherit from only one base class, so if you want to use abstract classes to provide polymorphism to a group of classes, they must all inherit from that class.
- Abstract classes may also provide members that have already been implemented. Therefore, you can ensure a certain amount of identical functionality with an abstract class, but cannot with an interface.
Here are some recommendations to help you to decide whether to use an interface or an abstract class to provide polymorphism for your components.
- If you anticipate creating multiple versions of your component, create an abstract class. Abstract classes provide a simple and easy way to version your components. By updating the base class, all inheriting
classes are automatically updated with the change. Interfaces, on the other hand, cannot be changed once created. If a new version of an interface is required, you must create a whole new interface. - If the functionality you are creating will be useful across a wide range of disparate objects, use an interface. Abstract classes should be used primarily for objects that are closely related, whereas interfaces
are best suited for providing common functionality to unrelated classes. - If you are designing small, concise bits of functionality, use interfaces. If you are designing large functional units, use an abstract class.
- If you want to provide common, implemented functionality among all implementations of your component, use an abstract class. Abstract classes allow you to partially implement your class, whereas interfaces
contain no implementation for any members.
版权声明:本文博主原创文章。博客,未经同意不得转载。
C# - Recommendations for Abstract Classes vs. Interfaces的更多相关文章
- Part 33 Difference between abstract classes and interfaces
- C# - Abstract Classes
Abstract classes are closely related to interfaces. They are classes that cannot be instantiated, ...
- Effective Java 18 Prefer interfaces to abstract classes
Feature Interface Abstract class Defining a type that permits multiple implementations Y Y Permitted ...
- Why does Typescript use the keyword “export” to make classes and interfaces public?
原文: https://stackoverflow.com/questions/15760462/why-does-typescript-use-the-keyword-export-to-make- ...
- androidstudio出包问题--Warning: there were 1 unresolved references to classes or interfaces.
问题:存在unresolved的类或接口导致打包失败 Warning: there were 1 unresolved references to classes or interfaces. You ...
- Part 32 Abstract classes in c#
- Class Abstraction -- Object Interfaces
<?php /* PHP 5 introduces abstract classes and methods. Classes defined as abstract may not be in ...
- What’s the difference between an interface and an abstract class in Java?
原文 What’s the difference between an interface and an abstract class in Java? It’s best to start answ ...
- Java抽象类(Abstract Class)与接口(Interface)区别
抽象类与接口比较 抽象类跟接口类似,都不能实例化,可能包含不需实现方法或已实现的方法. 抽象类可以定义一些不是静态或常量的字段,定义 public, protected, private访问级别的具体 ...
随机推荐
- Java线程学习笔记(一个)
一个.正在创建的线程: 老掉牙的话题了.继承 java.lang.Thread父类或者实现Runnalbe接口.这里就提一句: class Thread implements Runnable Thr ...
- VS2010使整个过程说明了安装包
该项目的第一个版本出来,要成为一个包,很长一段时间没做了一些被遗忘,上差了差资料,写了一个,总结下,可能还不是非常完好,仅作參考. 1.首先在打开 VS2010 >新建>项目 2.创 ...
- c++日历v1.12版
////////////////////////////新增信息修改功能,未完善. #include<iostream> #include <string> #include& ...
- mysql语句中使用like后面的%(百分号)的问题
问题:mysql语句中使用like后面的%(百分号) 是不是越多运行效率越慢! 总用时:0.0489秒 0.0691 0.0485 0.0467 SELECT `goods_name`, `goods ...
- 日积月累:weightSum和layout_weight属性合用
解说一:weightSum和layout_weight属性合用 android:weightSum属性:定义weight总和的最大值. 假设为指定该值,全部子视图的layout_weight属性的累加 ...
- mysql数据文件迁移到新的硬盘分区的方法
该系统增加了一个硬盘.要创建新的分区/data文件夹,mysql对于数据文件夹/var/lib/mysql 1. 停止mysql维修 [root@localhost~]# service mysql ...
- 【Hibernate步步为营】--(一对多映射)之双向关联
上篇文章讨论了单向关联的一对多映射,在一的一端维护双向的关系这样的做法尽管能实现可是存在非常多缺陷,首先生成非常多多余的SQL语句,由于多的一端不维护关系,仅仅有一的一端维护,在进行操作时一的一端会发 ...
- 跨域 Iframe 通信解决方案(兼容 IE 系列浏览器。)
实现思路: 1.postMessage(IE8+, Firefox 3.1+, Opera 9+, Safari, and Chrome) 2.利用window.navigator共享信息,使支持IE ...
- java它们的定义jar套餐读Excel(这包括2003和2007)数据,和实例
使用java它们的定义jar套餐读excel数据支持excel2007和excel2003 在http://download.csdn.net/detail/u010792467/8079355下载所 ...
- 亚马逊记AWS(Amazon Web Services)自由EC2应用
很长时间,我听到AWS能够应用,但是需要结合信用卡,最近申请了. 说是免费的,我还是扣6.28,后来我上网查了.认为是通过进行验证.像服务期满将返回. 关键是不要让我进入全抵扣信用卡支付passwor ...