Abstract Classes vs Interfaces in C#

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.

   public abstract class ToastBase
    {
        public ToastBase()
        {
            Console.WriteLine("Toast bread in toaster");
        }

        public abstract void AddSpread();
    }

    public class PeanutButterToast : ToastBase
    { 
        public override void AddSpread()
        {
            Console.WriteLine("Spread peanut butter on toast");
        }
    }

    public class JellyToast : ToastBase
    {
        public override void AddSpread()
        {
            Console.WriteLine("Spread jelly on toast");
        }
    }

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:

  public abstract class PaymentBase
    {

        public void ProcessPayment(int paymentID)
        {
            PaymentProcessor.SendPaymentRequest(paymentID);
            PaymentProcessor.HandlePaymentResult(paymentID);             
        }

        public abstract void CollectCardInfo();
    }

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.

   public interface IOrder
    {
       int OrderID { get; set; }
       decimal orderAmount { get; set; }
    }

    public interface ICustomer
    {
        void PlaceOrder(IOrder order);
    }
   
    public class Customer : ICustomer
    {
        public void PlaceOrder(IOrder order)
        {
            //work with order properties, etc
            int i = order.OrderID;
        }
    }

    public class LoyalCustomer : ICustomer
    {
        public void PlaceOrder(IOrder order)
        {
            //apply discount
            order.orderAmount -= 5;
        }
    }

That’s all for now.  I hope you enjoyed this post!

 

 

 

 

 

 

 

 

CategoriesC#

Leave a Reply