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的更多相关文章

  1. Part 33 Difference between abstract classes and interfaces

  2. C# - Abstract Classes

     Abstract classes are closely related to interfaces. They are classes that cannot be instantiated, ...

  3. Effective Java 18 Prefer interfaces to abstract classes

    Feature Interface Abstract class Defining a type that permits multiple implementations Y Y Permitted ...

  4. 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- ...

  5. androidstudio出包问题--Warning: there were 1 unresolved references to classes or interfaces.

    问题:存在unresolved的类或接口导致打包失败 Warning: there were 1 unresolved references to classes or interfaces. You ...

  6. Part 32 Abstract classes in c#

  7. Class Abstraction -- Object Interfaces

    <?php /* PHP 5 introduces abstract classes and methods. Classes defined as abstract may not be in ...

  8. 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 ...

  9. Java抽象类(Abstract Class)与接口(Interface)区别

    抽象类与接口比较 抽象类跟接口类似,都不能实例化,可能包含不需实现方法或已实现的方法. 抽象类可以定义一些不是静态或常量的字段,定义 public, protected, private访问级别的具体 ...

随机推荐

  1. POJ3061 Subsequence(二进制前缀和法律+仿真足)

    二分法+前缀和法律 满足子序列长度的条件(0,n)之间,sum[x+i]-sum[i]从i元素开始序列长度x和.前缀和可在O(n)的时间内统计 sum[i]的值.再用二分找出满足条件的最小的子序列长度 ...

  2. jxl创Excel档java示例代码说明

    记得下载 并 导入jxl.jar 包,免积分下载地址:http://download.csdn.net/detail/u010011052/7561041 package Test; import j ...

  3. android代码集EditText只要输入号码、信

     如何设置EditText,因此,只有某些数字或字母可以进入它? 一.建立EditText,只要输入号码: 办法1:直接生成DigitsKeyListener了. et_1.setKeyListe ...

  4. 数据结构(C达到)------- 双链表

    双链表中的每个节点包含两个指针域,指针域包含其后继节点的内存地址,还有一个指针所存储的存储器地址其领域前驱节点. 双向链表结点的类型描写叙述: //双向链表的类型描写叙述 typedef int El ...

  5. C#使用ServiceController控制windows服务

    C#在,使用ServiceController控制类windows服务,添加首次使用前引文:System.ServiceProcess,空间中引用:using System.ServiceProces ...

  6. Linux的selinux

    SELinux 操作模式     学科 (Subject):SELinux 序,因此你能够将『主体』跟 process 划上等号:     目标 (Object): 主体程序是否能存取的『目标资源』一 ...

  7. Android Volley 之自己定义Request

    转载标明出处:http://blog.csdn.net/lmj623565791/article/details/24589837 今天群里一哥们须要自己定义Volley的Request的样例,于是产 ...

  8. jQuery简要dom操作

    文本 dom 获取标签 $(选择). 创建一个标签对象 $("标签"): 由于所有的返回jQuery对象,能够调用链(无论jQuery API 回报jQuery对象) 插入标签 内 ...

  9. Zend_Db_Table::getDefaultAdapter is not working

    在Bootstrap中使用 $url = constant ( "APPLICATION_PATH" ) . DIRECTORY_SEPARATOR . 'configs' . D ...

  10. net网站运行在自定义的Web服务器上

    ASP.NET 开发必备知识点(1):如何让Asp.net网站运行在自定义的Web服务器上   一.前言 大家都知道,在之前,我们Asp.net 的网站都只能部署在IIS上,并且IIS也只存在于Win ...