Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

C# - Difference between Abstract class and Interface in Asp.Net

Apr 6, 2010
In previous article I explained complete OOPS Concept. Now I will explain differences between Abstract classes and Interfaces.

An Abstract class without any implementation just looks like an Interface; however there are lot of differences than similarities between an Abstract class and an Interface. Let's explain both concepts and compare their similarities and differences.

What is an Abstract Class?

An abstract class is a special kind of class that cannot be instantiated. So the question is why we need a class that cannot be instantiated? An abstract class is only to be sub-classed (inherited from). In other words, it only allows other classes to inherit from it but cannot be instantiated. The advantage is that it enforces certain hierarchies for all the subclasses. In simple words, it is a kind of contract that forces all the subclasses to carry on the same hierarchies or standards.

What is an Interface

An interface is not a class. It is an entity that is defined by the word Interface. An interface has no implementation; it only has the signature or in other words, just the definition of the methods without the body. As one of the similarities to Abstract class, it is a contract that is used to define hierarchies for all subclasses or it defines specific set of methods and their arguments. The main difference between them is that a class can implement more than one interface but can only inherit from one abstract class. Since C# doesn't support multiple inheritance, interfaces are used to implement multiple inheritance.

When we create an interface, we are basically creating a set of methods without any implementation that must be overridden by the implemented classes. The advantage is that it provides a way for a class to be a part of two classes: one from inheritance hierarchy and one from the interface.

When we create an abstract class, we are creating a base class that might have one or more completed methods but at least one or more methods are left uncompleted and declared abstract. If all the methods of an abstract class are uncompleted then it is same as an interface. The purpose of an abstract class is to provide a base class definition for how a set of derived classes will work and then allow the programmers to fill the implementation in the derived classes.
Feature
Interface
Abstract class
Multiple inheritance
A class may inherit several interfaces.
A class may inherit only one abstract class.
Default implementation
An interface cannot provide any code, just the signature.
An abstract class can provide complete, default code and/or just the details that have to be overridden.
Access Modfiers
An interface cannot have access modifiers for the subs, functions, properties etc everything is assumed as public
An abstract class can contain access modifiers for the subs, functions, properties
Core VS Peripheral
Interfaces are used to define the peripheral abilities of a class. In other words both Human and Vehicle can inherit from a IMovable interface.
An abstract class defines the core identity of a class and there it is used for objects of the same type.
Homogeneity
If various implementations only share method signatures then it is better to use Interfaces.
If various implementations are of the same kind and use common behaviour or status then abstract class is better to use.
Speed
Requires more time to find the actual method in the corresponding classes.
Fast
Adding functionality (Versioning)
If we add a new method to an Interface then we have to track down all the implementations of the interface and define implementation for the new method.
If we add a new method to an abstract class then we have the option of providing default implementation and therefore all the existing code might work properly.
Fields and Constants
No fields can be defined in interfaces
An abstract class can have fields and constrants defined

 I have explained the differences between an abstract class and an interface.

I think it helps you  

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

37 comments :

Anonymous said...

its really helpful...Thanx..

Anonymous said...

Thank u!!

Venkatesh said...

Good Explanation...:)

Anonymous said...

good

Spice said...

Give Example Dude!!!

Basavaraja K said...

Good Expaination...

Anonymous said...

good work!

Anonymous said...

awesOme !!

expert advice said...

Amazing work!

Anonymous said...

nice explanation..... thank you very much

Harikiran said...

good....

account said...

Now, I've known about the differences. thanks!

Anonymous said...

very nice article indeed

Anonymous said...

good explanation.....tanq....!!

Anonymous said...

Helpful...
thanx.

manoj kumar said...

remove my confusion

Anonymous said...

Awesome explanation yaar, thanks.

-Abdul Samathu K

HiMa Patel said...

keep it up....gud 1 4 beginners...

java training in chennai said...

1. A class can implement more than one interface(Multiple Inheritance is possible with interface). But A class can not extend more than one abstract class(Multiple Inheritance is not possible with class).

2. If You want to add a new method in interface, you need to implement that method in all the classes that implementing the interface. But if you want to add a new method in abstract class, you can add it as a non abstract method. You don't need to implement it in all the classes that extending the abstract class.

3. Interface don't have constructors. But Abstract class has constructors.

.Net Training in Chennai said...

Nice Explanation on abstract class and interface.

Anonymous said...

Can we create interfaces in abstract class?

raj kumar said...

Hi Suresh Sir,

I need your help, which situation used interface and abstract class. Please post any bady

Anonymous said...

It's really helpful... I have faced this question in most of the interiviews..

Thanks,
Sam N R

Bala Murugan said...

i have understand the concepts but Can you explain through example....

Anonymous said...

Really bad:-)

Anonymous said...

u just copy and pasted all data (even not change the words ) from code project.com check it----> http://www.codeproject.com/Articles/11155/Abstract-Class-versus-Interface

Anonymous said...

Best answer

Anonymous said...

well explained........

Anonymous said...

its very nice

munib qazi said...

Really gud :)

Bhuva Bhavesh said...

Thank You.....

vikas amballa said...

Now i clearly understand the concepts.....

vikas amballa said...
This comment has been removed by the author.
Fayaz Hussain said...
This comment has been removed by the author.
Fayaz Hussain said...
This comment has been removed by the author.
Fayaz Hussain said...
This comment has been removed by the author.
Fayaz Hussain said...
This comment has been removed by a blog administrator.

Give your Valuable Comments

Other Related Posts

© 2010-2012 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.