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.

版权声明:本文博主原创文章。博客,未经同意不得转载。

时间: 2024-07-29 03:49:25

C# - Recommendations for Abstract Classes vs. Interfaces的相关文章

C# - Abstract Classes

 Abstract classes are closely related to interfaces. They are classes that cannot be instantiated, and are frequently either partially implemented, or not at all implemented. One key difference between abstract classes and interfaces is that a clas

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

问题: Warning: there were 1 unresolved references to classes or interfaces. You may need to add missing library jars or update their versions. If your code works fine without the missing classes, you can suppress the warnings with '-dontwarn' options.

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 answering this question with a brief definition of abstract classes and interfaces and then explore the differences between the two. A class must be decla

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

抽象类与接口比较 抽象类跟接口类似,都不能实例化,可能包含不需实现方法或已实现的方法. 抽象类可以定义一些不是静态或常量的字段,定义 public, protected, private访问级别的具体方法. 接口的所有字段自动是public.静态.常量,所有定义的方法的访问级别都是public. 类只能继承一个抽象类,可以实现多个接口. 抽象类使用场景 1.你想在几个密切相关的类共享代码. 2.你想让继承你抽象类的类有一些共用的字段或方法,或想设置protected, private的访问级别.

C++ Core Guidelines

C++ Core Guidelines September 9, 2015 Editors: Bjarne Stroustrup Herb Sutter This document is a very early draft. It is inkorrekt, incompleat, and pµøoorly formatted. Had it been an open source (code) project, this would have been release 0.6. Copy

C# for Beginner session 31 to 44

Part 31 - C# Tutorial - Explicit interfaces implementation Part 32 - C# Tutorial - Abstract classes in c# Part 33 - C# Tutorial - Difference between abstract classes and interfaces Part 34 - C# Tutorial - Problems of multiple class inheritance Part 3

java基础之 Advanced Class Design

 java基础之 Advanced Class Design Abstract Classes In many programming situations, you want to specify an abstraction without specifying implementation-level details. In such cases, you can use either abstract classes or interfaces. Abstract classes are

amazon 汇总

1. what is hash table? how to implement in C? how to deal with conflicts? (linkedlist, linear probing, double hashing...etc)什么是 hashtable, hash function, key为string怎么办.Describe how hash table works? In computing, a hash table (also hash map) is a dat

基础知识(1)- Java SE 8 Programmer I (1z0-808)

Java Basics  Define the scope of variables Define the structure of a Java class Create executable Java applications with a main method; run a Java program from the command line; including console output. Import other Java packages to make them access