tags:

views:

159

answers:

5

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 ,

+8  A: 

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.

dcp
+2  A: 

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

Dan Bryant
+6  A: 

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.

JSBangs
+1 . the question is "stupid" as it is the same as "what is better - pepper or salt". THere is a reason for both, so clearly nooone is better. If that would the case, there would not be both options as the C# langauge designer is a very smart man.
TomTom
The question doesn't ask which is "better" so it isn't "stupid"
Chris Nicola
...it is however a question that has been asked a million times before so in that respect...
Chris Nicola
A: 

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.

Matt Greer
+1  A: 

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.

Brian Gideon
I agree, these concerns are ignored all too often. Interfaces are write once, never modify, although extension methods have added some ability to extend an interface. Abstract classes allow you to be more specific on the requirements too, for example, by implementing contracts or exception handling, which can't be done from an interface.
Mark H
Of course there is no correct answer here but we need to consider the principle of favouring composition over inheritence.In addition a class can always implement additional interfaces for extensibility without breaking backward compatibility but an abstract base class removes the inheritence choice in your derived class.
Daz Lewis