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.
|
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. |
|||
|
|||
56 comments :
its really helpful...Thanx..
Thank u!!
Good Explanation...:)
good
Give Example Dude!!!
Good Expaination...
good work!
awesOme !!
Amazing work!
nice explanation..... thank you very much
good....
Now, I've known about the differences. thanks!
very nice article indeed
good explanation.....tanq....!!
Helpful...
thanx.
remove my confusion
Awesome explanation yaar, thanks.
-Abdul Samathu K
Nice Explanation on abstract class and interface.
Can we create interfaces in abstract class?
Hi Suresh Sir,
I need your help, which situation used interface and abstract class. Please post any bady
It's really helpful... I have faced this question in most of the interiviews..
Thanks,
Sam N R
i have understand the concepts but Can you explain through example....
Really bad:-)
Best answer
well explained........
its very nice
Really gud :)
Thank You.....
Now i clearly understand the concepts.....
very thankfull to nice blog
example please
good... give some example for each poitn.....it will be more helpfull
its helpful...
Very Nice Article
Great work Suresh, i appretiate you
superb suresh..
fggf fg
Sir,
Explanation is nice. But it would be more better if you give example.
thankyou.....explanations are good
good sir
Nice article, thank u very much...
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?"
hi
What is all this advertising and rubbish on this page? Such a great article, but the webpage is awful.
Thank you....
thank u very much bro
Very helpful
Nice one
Thanks for this post.
Note: Only a member of this blog may post a comment.