I have been into C# and always feel confusion between what should be selected between interfaces and abstract classes. Can some one please help resolve this query ?
Thanks ,
I have been into C# and always feel confusion between what should be selected between interfaces and abstract classes. Can some one please help resolve this query ?
Thanks ,
Think of an interface like a contract
, you are specifying something that you are expecting the consumers of that interface to implement.
An abstract class, on the other hand, is useful when you have some of the code you need to implement for the class, but not all of it. And you can declare abstract methods for the parts that need to be implemented by the subclasses of the abstract class. Remember that abstract methods must be implemented by the subclasses, but you can also provide your own code within the class itself via normal private/public/protected/etc. methods.
So if you are just writing a contract that you want subclasses to implement, then think interface. But if you are writing something that's more of a "pattern", where you might have some of the method implementations (but not all) that will be common to all the child implementations, then you should think abstract class.
It depends on what you're trying to do. Abstract classes are good when you have common functionality with common state. Even if you use an abstract class, it's often helpful to provide an interface as well, since you don't always need access to the state and other classes could potentially implement the interface without that state (such as a test stub.)
If you only need helper methods (no state, simply composing method calls), then I prefer to use an interface with extension methods for the helper functions (such as overloads).
Neither is "better"--they have different purposes.
An interface is for when you need to define a set of common methods with similar semantics, but the classes that define those methods can't inherit from the same source, and might have completely different implementations.
An abstract class is for when you want to partially implement certain functionality, but delegate important parts to a subclass. The implementation of an abstract class is much more restricted than the implementation of an interface, since the implementation classes have to inherit from the abstract class, and they cannot override the parts of the base class that aren't virtual
or abstract
.
Abstract base classes are best used internally, for your own needs. When you are writing something that needs to interface with someone else's code, interfaces are better. The primary reason is C# does not support multiple inheritance.
So for example if you provide a PluginBase
abstract base class that you want consumers to subclass in order to provide plugins for your system, you have forced them into your base class and severely limited them. An IPlugin
interface is much more flexible.
There are already some code answers, but I want to add another perspective that a lot of developers forget about.
If you use an interface in a public API then you have committed yourself to those members it contains and only those members. If you attempt to add something to an interface then it is a version breaking change. Why? Because every class must implement every member in an interface. Code that uses your API will stop compiling.
Abstract classes do not suffer this same limitation since you can add methods and provide a reasonable default implementation for them. Subclasses are then none the wiser and would not be impacted by the change.