Abstract Classes Versus Interfaces

By Anthony Jackson. Filed in Software Development, Software Development Job Search  |  
TOP del.icio.us digg

Had a very interesting question on a recent interview: “What’s the difference between an abstract class and an interface? Why would you choose to use one over another?”

How exactly do you answer this question off the top of your head in an interview format and truly do the subject justice? I simply had to hope that the interviewer was more interested in thought processes and an abstract discussion than concrete information.

After having time to fully digest the question and put my thoughts together, here is my attempt at an intelligent answer, which may not be much better than my interview answer.

My biggest problem with this question is that it pre-supposes that the two are interchangeable. While an argument can be made that it is possible to use them interchangeably, it ignores their strengths, weaknesses and design purposes.

Abstract Class:
Full or Partial implementation
Inheriting classes may only inherit from one class
Updates passed along to inheriting classes
Great for classes that are similar (Dog and Cat both inherit from Animal)

Interfaces:
Only define input/output contract, no implementation
Classes may implement multiple interfaces
Once defined, should never change
Great for functionality used in many dis-similar classes

This still doesn’t quite answer the question, another good way to look at it is Nouns versus Verbs. Abstract Classes represent a Noun, such as in the example given above of Dog and Cat inheriting from Animal, while interfaces represent a Verb. If I were representing my dog Heidi as a class, she would inherit from Dog, but would need to implement the interfaces IJumpOnEveryone and IChewOnYourHand.

But in the end, I’m not sure we’ll come to a conclusion of what’s “right” and what’s “wrong” in this. While their are the obvious cases that neatly fit into either/or, as always I hate the cases where I can make a valid argument for either way. It’s times like those where I hope that one presents a clear disadvantage over the other to help me select.

I’d love to hear others opinions on this, please comment.

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.

One comment to “Abstract Classes Versus Interfaces”

TrackBacks / PingBacks

  1. Pingback
    Anthony Jackson's Software Development Blog » Abstract Classes Versus Interfaces Revisited

Leave a Reply

You must be logged in to post a comment.