Click here to Skip to main content
12,956,197 members (107,651 online)
Rate this:
Please Sign up or sign in to vote.
See more:

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

Could you please tell me Real time Example..

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

Solution 1

thatraja 28-Feb-11 11:00am
Well, good link for the OP.
Rate this: bad
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.

E$w@r 1-Mar-11 12:19pm
My vote to you 5!.
SAKryukov 1-Mar-11 13:48pm
Thank you,
Espen Harlinn 2-Mar-11 6:41am
Good points, a 5
SAKryukov 2-Mar-11 19:59pm
Thank you.
Rate this: bad
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.

Espen Harlinn
SAKryukov 28-Feb-11 21:09pm
Not bad, my 5. Please see: I added a note on multiple inheritance which is fundamentally important.
Espen Harlinn 1-Mar-11 12:21pm
Thank you, SAKryukov :)
E$w@r 1-Mar-11 12:17pm
Good one, my 5.
Espen Harlinn 1-Mar-11 12:22pm
Thank you, E$w@r :)
Rate this: bad
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.
Rate this: bad
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.

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

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 6,849
CHill60 3,550
Maciej Los 3,223
ppolymorphe 2,060
Jochen Arndt 1,975

Advertise | Privacy | Mobile
Web02 | 2.8.170525.1 | Last Updated 23 Mar 2014
Copyright © CodeProject, 1999-2017
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