When should you use an abstract class, when should you use an interface? Here’s the lowdown on some of the reasons to choose one over another. First, take a look at a very simple example of an abstract class and two classes that inherit from it.
Setting aside technical reasons, one of the strongest benefits to using an abstract class is the ability to provide an invariant implementation of some functionality across all sub classes, while leaving other implementations up to the subclass. In our example above, all types of toast must first be toasted in a toaster. Then, depending on the type of toast, the subclass can add whatever type of spread it wants.
On to the technical side. An abstract class cannot be instantiated. It is meant to be inherited. If a method is not marked as abstract in an abstract class, the implementation belongs to the abstract class. Abstract methods are implicitly virtual, and are meant to be overridden by the subclass. They have no implementation in the base class, and therefore no method body, just a signature. An class can inherit from multiple interfaces, but only one abstract class. Here’s a more real world example. Imagine a website back end where we want to collect and process credit cards for online purchases. We might implement an abstract class such as the following:
The abstract PaymentBase class dictates that no matter what way we collect card information in the front end website, we must always process the payment the same way.
Interfaces are like contracts that dictate all the functionality a class must have that implements it. When you implement an interface, you must define a method body for all the method signatures in the definition. Interfaces are great for dependency injection, which states that we should always program to an interface, not an implementation. This is beyond the scope of this article, but warrants further reading if you’re not familiar with it.
Interfaces enable you do decorate additional classes of the same concept, adding functionality. Consider the example below. We have a Customer and a LoyalCustomer class. They both implement the ICustomer interface. While we can place an order for each type of customer using the same interface, we can apply a discount to a loyal customer. Meanwhile, all throughout our application, we can use the ICustomer interface anywhere we refer to a customer, and pass either Customer or LoyalCustomer to the method. This also allows us to extend Customer to additional custom types down the road.
That’s all for now. I hope you enjoyed this post!