Thursday, June 18, 2009

Differences between an abstract class and an interface in C#

Most of the new developers are always confused as when, where and why we should use an interface or an abstract class.Although both abstract classes and interfaces serve the same purpose to some extent but yet they are different in many terms.

Although both act as a sort of contract but there are some major differences in both of them which i will sum up in this article

  1. Interfaces belong to interface type where as abstract classes belong to class type
  2. Interface contain only abstract methods which the type who implements the interface should give its declaration whereas abstract class may contain both abstract methods as well as concrete methods.
  3. Abstract class can extend another class and implement multiple interfaces, where as an interface cannot be inherited from a class.
  4. If A is your abstract class and B is a class which extends A, then in this situation you can have B only extend from A not from any other class i.e no direct multiple inheritance is supported and this in a sense is achieved by interfaces as we can implement any no of interfaces for a given class.
  5. Both follow a different paradigm as interfaces are implemented  whereas classes are extended.

So both concepts are a kind of generalization and are used in situations where we cannot in reality define the object which we are talking about this line means for example if we say a car then its a very general specification and no such entity exists in real which we can say is a car.But if you say “Ferrari” then it does make sense and you can identify a unique instance.

So to define car’s action  we can use either abstract classes or  interfaces to declare the general car functionalities.

After doing that we can go ahead and create a Ferrari class which either can derive from your abstract class or can implement the interface created by you.

Using abstract classes or interfaces totally depends on what type of inheritance are you following if your applications is sounding to use multiple inheritance then its always better to go with interfaces and if you know that with a particular class the derived class need not be derived from another class then you can go ahead with abstract classes.

These are the main differences and some similarities between these two.If you want to add something else do leave a comment.

Happy Programming!!!!!!!

4 comments :

drbcladd said...

Most of the differences you cite are syntactic rather than substantive.

That there is no direct multiple inheritance is the big difference: a class can choose to implement any number of interfaces and any class which does implement a given interface can be provided when an object of that interface type is required.

One big win in an abstract class is being able to factor out common behavior or even common implementation of some part of the class even when there is no reasonable default for the extension point methods.

I would love to see a discussion of when you choose to use one or the other. In particular, if either could be used (we are not required to extend some _other_ class), how to you decide?

Wolfgang Loder said...

Guess the 'new developer' will still be confused after reading your post.
(1) Your title should read 'Differences between an abstract class and an interface in C#'. Other languages, like C++, have different constructs and use pure abstract classes as 'interface'
(2) The main difference between an interface and a class is that the interface describes a beahviour and the class an entity with state.
(3) An abstract class is still an entity with state, but does not implement all methods.
(4) That C# (or Java, for that matter) uses interfaces as workaround to achieve multiple inheritance, has nothing to do with the pattern. It can be seen either as defect in the language or deliberately left out, because multiple inheritance is a design defect.

This leaves us with the question, when to use abstract classes? Whenever you have a behaviour that demands certain state information (fields/properties) to be present. For example, an abstract class might have data fields implemented for versioning purposes.

It's an old discussion and the opinions have a wide range. What is important though, to have one and only one guideline in a team to which everybody adheres no matter if they agree or not.

Aashish said...

@Wolfgang Loder
Yes you are right i left out to include C# in the posts title which i have done now.

Thanks for sharing your views

simi said...

hi,

First of all. Thanks very much for your useful post.

I just came across your blog and wanted to drop you a note telling you how impressed I

was with the information you have posted here.

Please let me introduce you some info related to this post and I hope that it is useful

for .Net community.

There is a good C# resource site, Have alook

http://www.csharptalk.com/2009/09/c-class.html
http://www.csharptalk.com

simi

Post a Comment