Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: General
Hi
 
When we use abstract and when we use interface...?
 
Could you please tell me Real time Example..
 

 
Thanks and Regards
venkat
Posted 28-Feb-11 3:04am
Comments
AlbinAbel at 28-Feb-11 9:55am
   
This question asked many times here
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Comments
thatraja at 28-Feb-11 11:00am
   
Well, good link for the OP.
5!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  
v3
Comments
E$w@r at 1-Mar-11 12:19pm
   
My vote to you 5!.
SAKryukov at 1-Mar-11 13:48pm
   
Thank you,
--SA
Espen Harlinn at 2-Mar-11 6:41am
   
Good points, a 5
SAKryukov at 2-Mar-11 19:59pm
   
Thank you.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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
  Permalink  
Comments
SAKryukov at 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 at 1-Mar-11 12:21pm
   
Thank you, SAKryukov :)
E$w@r at 1-Mar-11 12:17pm
   
Good one, my 5.
Espen Harlinn at 1-Mar-11 12:22pm
   
Thank you, E$w@r :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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.
  Permalink  
v2

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

  Print Answers RSS
0 OriginalGriff 235
1 Sergey Alexandrovich Kryukov 120
2 Richard MacCutchan 100
3 ChauhanAjay 95
4 BotCar 95
0 Sergey Alexandrovich Kryukov 9,056
1 OriginalGriff 8,041
2 CPallini 2,613
3 Richard MacCutchan 2,221
4 Abhinav S 1,928


Advertise | Privacy | Mobile
Web01 | 2.8.140827.1 | Last Updated 23 Mar 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100