How to decide between an Interface or Base Class for an new implementation?
A base class, abstract or not, can contain implemented members. An interface cannot. If all of your implementations are going to perform similarly, a base class might be the way to go because all of your child classes can share the same implementations of the members on the base class. If they aren't going to share implementations, then an interface might be the way to go.
Example:
class Person
{
string Name { get; set; }
}
class Employee : Person
{
string Company { get; set; }
}
It makes sense for Employee to inherit from Person because the Employee class doesn't have to define a Name
property because it shares the implementation.
interface IPolygon
{
double CalculateArea();
}
class Rectangle : IPolygon
{
double Width { get; set; }
double Height { get; set; }
double CalculateArea()
{
return this.Width * this.Height;
}
}
class Triangle : IPolygon
{
double Base { get; set; }
double Height { get; set; }
double CalculateArea()
{
return 0.5 * this.Base * this.Height;
}
}
Because Rectangle
and Triangle
have such differing implementations of CalculateArea
, it doesn't make sense for them to inherit from a base class.
If you make a base class, and find that in only contains abstract members, you may as well just use an interface.
And, as j__m states, you cannot inherit from multiple base classes, but you can implement multiple interfaces.
I usually define interfaces first, and if I find myself duplicating code in my implementations, I create a base class that implements the interface, and make my implementations inherit from it.
To decide whether to use an abstract class or an interface, I find this article very helpful Source:
A good way to distinguish between a case for the one or the other for me has always been the following:
Are there many classes that can be "grouped together" and described by one noun? If so, have an abstract class by the name of this noun, and inherit the classes from it. (A key decider is that these classes share functionality, and you would never instantiate just an Animal... you would always instantiate a certain kind of Animal: an implementation of your Animal base class) Example: Cat and Dog can both inherit from abstract class Animal, and this abstract base class will implement a method void Breathe() which all animals will thus do in exactly the same fashion. (I might make this method virtual so that I can override it for certain animals, like Fish, which does not breath the same as most animals).
What kinds of verbs can be applied to my class, that might in general also be applied to others? Create an interface for each of these verbs. Example: All animals can be fed, so I will create an interface called IFeedable and have Animal implement that. Only Dog and Horse are nice enough though to implement ILikeable - I will not implement this on the base class, since this does not apply to Cat.
Please also look at this Interface vs Base class question.