This may be a generic OOP question. I wanted to do a generic comparison between an interface and an abstract class on the basis of their usage.
When wou
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.
Copied from:
http://msdn.microsoft.com/en-us/library/scsyfw1d%28v=vs.71%29.aspx
My two cents:
An interface basically defines a contract, that any implementing class must adhere to(implement the interface members). It does not contain any code.
On the other hand, an abstract class can contain code, and there might be some methods marked as abstract which an inheriting class must implement.
The rare situations I've used abstract classes is when i have some default functionality that the inheriting class might not be interesting in overriding, in say an abstract base class, that some specialized classes inherit from.
Example(a very rudimentary one!):Consider a base class called Customer which has abstract methods like CalculatePayment()
, CalculateRewardPoints()
and some non-abstract methods like GetName()
, SavePaymentDetails()
.
Specialized classes like RegularCustomer
and GoldCustomer
will inherit from the Customer
base class and implement their own CalculatePayment()
and CalculateRewardPoints()
method logic, but re-use the GetName()
and SavePaymentDetails()
methods.
You can add more functionality to an abstract class(non abstract methods that is) without affecting child classes which were using an older version. Whereas adding methods to an interface would affect all classes implementing it as they would now need to implement the newly added interface members.
An abstract class with all abstract members would be similar to an interface.
in java you can inherit from one (abstract) class to "provide" functionality and you can implement many interfaces to "ensure" functionality
One interesting location where interfaces fare better than abstract classes is when you need to add extra functionality to a group of (related or unrelated) objects. If you cannot give them a base abstract class (e.g., they are sealed
or already have a parent), you can give them a dummy (empty) interface instead, and then simply write extension methods for that interface.
When to prefer an abstract class over interface?
When to prefer an interface over abstract class?
I wrote an article of when to use an abstract class and when to use an interface. There is a lot more of a difference between them other than "one IS-A... and one CAN-DO...". To me, those are canned answers. I mention a few reasons when to use either of them. Hope it helps.
http://codeofdoom.com/wordpress/2009/02/12/learn-this-when-to-use-an-abstract-class-and-an-interface/