Abstract Classes Versus Interfaces Revisited

By Anthony Jackson. Filed in C#, Software Development  |  
TOP del.icio.us digg

In the original article, Abstract Classes Versus Interfaces, I came to the conclusion that each one has clear cut cases that strongly present themselves, as well as more ambiguous cases that could go either way. I’d now like to change my opinion.

I am now in favor of both. Yes, that’s right I now think that the best option in most cases is to use an Interface along with an Abstract class that is never inherited from. While this sounds strange on the surface, it’s due to Microsoft Code Contracts.

Basically, this technology helps with writing Guard Clauses in your code, i.e. making sure that parameters are not passed in as null or that integer values fall within a certain range. It has the ability to take things one step further and ensure that your code is written to enforce rules. For example, if a value must always be a positive value, your able to ensure that not only code that you currently have enforces this, but any code added at a later time also enforces it.

But back to Interfaces and Abstracts, Microsoft Code Contracts allow you to define an abstract class which implements an Interface but is never intended to be instantiated. Instead, this class contains all of your Guard Clauses. The Interface and Abstract Class are combined to create a complete package “inherited” by any class that implements the Interface, courtesy of Microsoft Code Contracts.

Suddenly we have the capability to extend interfaces in a way that probably should have been included in the framework/languages in the first place. We can now not only specify properties and data types, but ensure that their values are always valid.

Rather than go through how to actually implement Microsoft Code Contracts, I’m going to point you to an excellent series of articles I found on the subject. Start with Code Contracts Part 1 for introductory information or skip right to the section that shows how to do what I discussed above with the fifth part of this series, Abstract Types and Interfaces.

Hopefully you’re as excited about this important evolutionary step in software development as I am.

About Anthony Jackson

Anthony Jackson has written 10 posts in this blog.

Anthony has been developing software professionally since 1996; working primarily with Microsoft technologies such as C#, VB.NET, ASP.NET and MS SQL Server. He has a varied background working in a number of industries, on teams of different sizes including Enterprise Application Development. He often takes on varied roles for projects as well, quickly adapting to what is needed for a given project and is involved in the full life-cycle of software development. This blog and it's articles are entirely the opinion of the author Anthony Jackson and do not reflect the opinions of his employers or clients past, present or future. He currently works for Avanade as a Senior System Analyst and loves working their, but everything written is still just his opinion and does not necessary reflect on any of his employers or clients past, present or future.

Leave a Reply

You must be logged in to post a comment.