Click here to Skip to main content
15,881,173 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi

When we use abstract and when we use interface...?

Could you please tell me Real time Example..



Thanks and Regards
venkat
Posted
Comments
Albin Abel 28-Feb-11 9:55am    
This question asked many times here

 
Share this answer
 
Comments
thatraja 28-Feb-11 11:00am    
Well, good link for the OP.
5!
The question is asked many times. I want to show old Answers, because they are interesting, as well as discussion. I already gave my Answer, but see for others as well.

Please see the discussion on how to decide on use of interfaces vs. abstract classes: How to decide to choose Abstract class or an Interface[^].

Also, one fundamental difference is that interfaces allows for multiple inheritance, in contrast to classes (abstract or not). This is called week form of multiple inheritance and cause serious design implications.

—SA
 
Share this answer
 
v3
Comments
TweakBird 1-Mar-11 12:19pm    
My vote to you 5!.
Sergey Alexandrovich Kryukov 1-Mar-11 13:48pm    
Thank you,
--SA
Espen Harlinn 2-Mar-11 6:41am    
Good points, a 5
Sergey Alexandrovich Kryukov 2-Mar-11 19:59pm    
Thank you.
--SA
An interface is as the name implies an interface to an implementation, a while an abstract method is a virtual method without an implementation.

You use an abstract method (or property) when there is no logical default behavior for a virtual method.

You use an interface when you want different implementations to expose the same interface to the outside world.

One example would be to create interfaces exposing the data-model of a solution. One implementation could work directly with SQL server while another could work with a mid-tier server. It would be beneficial to the development process if one could seamlessly be interchanged with the other, as it’s probable that the one working directly with SQL server could be developed in a shorter amount of time, and later be reused to implement the mid-tier.

As long as the rest of the solution only sees interfaces exposing the same behavior externally there is a fair chance that the solution could be changed from a two-tier to a three-tier solution requiring little or no changes to the rest of the solution.

Regards
Espen Harlinn
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 28-Feb-11 21:09pm    
Not bad, my 5. Please see: I added a note on multiple inheritance which is fundamentally important.
--SA
Espen Harlinn 1-Mar-11 12:21pm    
Thank you, SAKryukov :)
TweakBird 1-Mar-11 12:17pm    
Good one, my 5.
Espen Harlinn 1-Mar-11 12:22pm    
Thank you, E$w@r :)
Including Dylan, Espen and SA answers I will also suggest you to read this-[When to use an Abstract Class and an Interface][^] article. this article reflects better understanding with examples.
 
Share this answer
 
When we are designing a large complex systems we can use abstract class.This will provide some common behavior to the subsystems.Any change can accommodate easily with abstract classes and the subclasses will be updated accordingly.

If it is a small system interfaces are good.Any change in the interface has to make changes in the implemented classes.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900