My two cents on abstract class vs interface

Understand the concept of interfaces and abstract classes to design applications that are extendable and loosely coupled

Interface vs abstract class

Interface vs abstract class

When designing applications, there are certain considerations you need to take care of when choosing whether you should use a abstract class or an interface. This choice of whether to design your functionality as an interface or an abstract class depends on certain factors. In this blog post I'll present a discussion on these points.

Abstract Class

First off, what is the difference between an abstract class and an interface? An abstract class is a special type of a class that cannot be instantiated. An abstract class should, however, be inherited by its subclasses. Abstract classes are either partially implemented or not implemented at all. You can have functionality in your abstract class -- the methods in an abstract class can be both abstract and concrete. An abstract class can have constructors -- this is one major difference between an abstract class and an interface. You may take advantage of abstract classes to design components and specify some level of common functionality in the methods of the abstract class until a specific implementation of the abstract class is needed in the class hierarchy.

Interface

An interface is basically a contract -- it doesn't have any implementation. An interface can contain only method declaration; it cannot contain method definition. You also can't have any member data in an interface. You can only have declaration of events, methods, and properties in an interface. An abstract class, on the other hand, can contain method definitions, fields and also constructors. Methods declared in an interface should be implemented by the classes that implement the interface. Note that a class can implement more than one interface but extend only one class. The class that implements the interface should implement all the members of an interface. Like abstract classes, an interface cannot be instantiated.

Should I use an abstract class or an interface?

Abstract classes provide you the flexibility to have certain concrete methods and some other methods that the derived classes should implement. On the contrary, if you use interfaces, you would need to implement all the methods in the class that extends the interface. An abstract class is a good choice if you have plans for future expansion -- if a future expansion is likely in the class hierarchy. If you would like to provide support for future expansion when using interfaces, you'll need to extend the interface and create a new one.

On a different note, it is easy to add a new interface to the hierarchy if need be. However, if you already have an abstract class in your hierarchy, you can't add another, i.e., you can only add an abstract class if there is not one available. You should use an interface if you want a contract on some behavior or functionality. You would not want to use an interface when you need to write the same code for the interface methods. In this case, you should use an abstract class and define the method once and reuse it as needed. You can use interfaces to decouple your application's code from any particular implementation of it. You can also use an interface to restrict access to members of a particular type. I'll discuss more about interfaces and abstract classes in my future posts here.

The MSDN states: "By using interfaces, you can, for example, include behavior from multiple sources in a class. That capability is important in C# because the language doesn't support multiple inheritance of classes. In addition, you must use an interface if you want to simulate inheritance for structs, because they can't actually inherit from another struct or class."

Implicit and explicit interface implementations

Interfaces can be implemented implicitly or explicitly. Let me explain how these two implementations differ. Consider an interface called IBusinessEntity.
public interface IBusinessLogic
{
void Initialize();
}
The following class named BusinessLogic implements this interface.
public class BusinessLogic : IBusinessLogic
{
public void Initialize ()
{
//Some code
}
}
You can create an instance of the BusinessLogic class and then call the Initialize() method as shown below.
IBusinessLogic businessLogic = new BusinessLogic();
businessLogic. Initialize();
The following code snippet illustrates how you can implement the IBusinessLogic interface implicitly.
public class BusinessLogic : IBusinessLogic
{
void IBusinessLogic.Initialize()
{

}
}
You can now invoke the Initialize () method the same way using a reference of the IBusinessLogic interface. The difference in the two approaches is that in the latter case, i.e., when you implement the interface explicitly in your class, you are constrained to invoking a method of your interface using a reference of the interface only. So, the following code snippet would not work  -- it won't compile at all.
BusinessLogic businessLogic = new BusinessLogic();
businessLogic.Initialize();

This article is published as part of the IDG Contributor Network. Want to Join?

From CIO: 8 Free Online Courses to Grow Your Tech Skills
Notice to our Readers
We're now using social media to take your comments and feedback. Learn more about this here.