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

56 comments :

Anonymous said...

its really helpful...Thanx..

Anonymous said...

Thank u!!

Venkatesh said...

Good Explanation...:)

Anonymous said...

good

Sharah 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

java training in chennai said...
This comment has been removed by a blog administrator.
.Net Training in Chennai said...

Nice Explanation on abstract class and interface.

Anonymous said...

Can we create interfaces in abstract class?

Unknown 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

Unknown said...

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

Anonymous said...

Really bad:-)

Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...

Best answer

Anonymous said...

well explained........

Anonymous said...

its very nice

Unknown said...

Really gud :)

Bhavesh Bhuva said...

Thank You.....

Unknown said...

Now i clearly understand the concepts.....

Unknown said...
This comment has been removed by the author.
Unknown said...
This comment has been removed by the author.
Unknown said...
This comment has been removed by the author.
Unknown said...
This comment has been removed by the author.
Unknown said...
This comment has been removed by a blog administrator.
Anonymous said...

very thankfull to nice blog

Anonymous said...

example please

Anonymous said...

good... give some example for each poitn.....it will be more helpfull

Unknown said...

its helpful...

Anonymous said...

Very Nice Article

Anonymous said...

Great work Suresh, i appretiate you

Anonymous said...

superb suresh..

Anonymous said...

fggf fg

Jatin said...

Sir,

Explanation is nice. But it would be more better if you give example.

Unknown said...

thankyou.....explanations are good

Abdul Qadeer khan said...

good sir

Anonymous said...

Nice article, thank u very much...

Shahbaz said...

Frist I would like to say thanks for this awesome article,
please tell me "is it necessary to implement every abstract method in derived class?"

Anonymous said...

hi

Anonymous said...

What is all this advertising and rubbish on this page? Such a great article, but the webpage is awful.

Unknown said...

Thank you....

Unknown said...

thank u very much bro

Anonymous said...

Very helpful

Anonymous said...

Nice one

Unknown said...

Thanks for this post.

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 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.