How can I decide whether to use an interface or an abstract class?

I was in C # and always have a confusion between what should be chosen between interfaces and abstract classes. Can anyone help resolve this issue?

Thanks,

+7
c # oop
source share
5 answers

Think of an interface, such as contract , you specify what you expect from consumers of this interface.

An abstract class, on the other hand, is useful if you have some code that needs to be implemented for the class, but not all. And you can declare abstract methods for parts that must be implemented by subclasses of the abstract class. Remember that abstract methods must be performed by subclasses, but you can also provide your own code inside the class itself through regular private / public / protected / etc. Methods

So, if you are just writing a contract that you want to implement subclasses, then think about the interface. But if you write something more than a "template" where you can have some of the method implementations (but not all) that will be common to all child implementations, then you should think about an abstract class.

+8
source share

Nothing is β€œbetter” - they have different goals.

  • The interface is designed to define a set of common methods with similar semantics, but the classes that define these methods cannot inherit from the same source and can have completely different implementations.

  • An abstract class is intended to partially implement certain functions, but to delegate important parts to a subclass. The implementation of an abstract class is much more limited than the implementation of an interface, since implementation classes must inherit from an abstract class and cannot override parts of the base class that are not virtual or abstract .

+6
source share

It depends on what you are trying to do. Abstract classes are good if you have common functionality with a common state. Even if you use an abstract class, it is often useful to also provide an interface, since you do not always need access to the state, and other classes can potentially implement the interface without this state (for example, a test stub).

If you need only helper methods (stateless, just making method calls), then I prefer to use the interface with extension methods for helper functions (for example, overloading).

+2
source share

There are already some answers to the code, but I want to add another perspective that many developers forget about.

If you use the interface in a public API, then you are dedicated to the members that it contains, and only to those members. If you try to add something to the interface, this will be a version change. What for? Because every class must implement every member of the interface. Code that uses your API will stop compiling.

Abstract classes do not suffer from the same restriction, since you can add methods and provide them with a reasonable default implementation. Subclasses are then no more wise and will not be affected by change.

+1
source share

Abstract base classes are best used internally for your own needs. When you write something that you need to interact with other people's code, the interfaces are better. The main reason is that C # does not support multiple inheritance.

So, for example, if you provide an abstract base class PluginBase that you want consumer subclasses to provide plugins for your system, you forced them into your base class and severely limited them. The IPlugin interface IPlugin much more flexible.

0
source share

All Articles