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访问级别的具体 ...
随机推荐
- struts 1.x 原理
Struts 当我接触到这个框架的时候.我就在想为什么是struts,而不是什么CraigFramework.结构.支撑,这样来理解也不难怪了. 为什么须要struts? 在struts in act ...
- Lua 环境结构 --Linux
curl -R -O http://www.lua.org/ftp/lua-5.2.3.tar.gz tar zxf lua-5.2.3.tar.gz cd lua-5.2.3 make linux ...
- js中推断对象详细类型
大家可能知道js中推断对象类型能够用typeof来推断. 看以下的情况 <script> alert(typeof 1);//number alert(typeof "2&quo ...
- 排序算法c语言描述---选择排序
排序算法系列学习,主要描述冒泡排序,选择排序,直接插入排序,希尔排序,堆排序,归并排序,快速排序等排序进行分析. 文章规划: 一.通过自己对排序算法本身的理解,对每个方法写个小测试程序. 具体思路分析 ...
- Ubuntu14.04设备JDK
1.设备JDK 打开命令直插式工具.输入以下三个命令: sudo add-apt-repository ppa:webupd8team/java sudo apt-get update sudo ap ...
- fzu 2150 Fire Game 【身手BFS】
称号:fzupid=2150"> 2150 Fire Game :给出一个m*n的图,'#'表示草坪,' . '表示空地,然后能够选择在随意的两个草坪格子点火.火每 1 s会向周围四个 ...
- 【Java】【Flume】Flume-NG源代码分析的启动过程(两)
本节分析配置文件的解析,即PollingPropertiesFileConfigurationProvider.FileWatcherRunnable.run中的eventBus.post(getCo ...
- Eclipse SDK构建J2EE开发环境
鄙视官Java EE Developers 体积庞大的兄弟们可以提出自己的J2EE开发环境! 1.第一次去Eclipse官网下载Eclipse IDE 我使用的是:Eclipse IDE for Ja ...
- 对于C11中的正則表達式的使用
Regular Expression Special Characters "."---Any single character(a "wildcard") & ...
- 谈到一些传统的企业网站SEO问题领域
在网络营销中的时间越长,有时候,企业网站还是有一些传统做法不解.也许,这是它的思想的局限.比如,我最近来到了一个新的工作环境中发现,虽然公司是专业从事传统渠道已经很不错了,但对于网络营销渠道还有改进的 ...