Link to home
Start Free TrialLog in
Avatar of infotechelg
infotechelgFlag for United States of America

asked on

Interface vs. Abstract

So, obviously, the next logical question would be when to use Interface and when to use Abstract.

I understand some of the differences between the two like abstract classes can have BOTH implementation and definitions, whereas interfaces cannot do implementation. abstract classes are inherited whereas interfaces are implemented.

Besides that, abstract and interface seem to serve the same purpose.

So, when do I use "abstract" and when do I use "interface"?

Thanks!!
ASKER CERTIFIED SOLUTION
Avatar of Jens Fiederer
Jens Fiederer
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Dmitry G
About purpose - not exactly. I just don't want to repeat myself but have a look at my post in your another thread, about IComparable. Abstract classes are not so useful here. Honestly, in my practice I use interfaces much more often than abstract classes.

https://www.experts-exchange.com/questions/26922834/Why-Interfaces.html
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I don't deny that the lines between interfaces and abstract classes are kind of gray. There are times when you can achieve the same functionality by using either mechanism.
Most of the time, during your design, it will probably make more sense to use abstraction vs. inheritance.

Well that didn't make one bit of sense, did it? What I meant to say was, "... it will probably make more sense to use interfaces vs. abstraction."


And for this one, I must have been tired...


interface IPostage   // Not PostageRate  : \
{
    double PostageRate { get; set; }
}

Open in new window

Avatar of infotechelg

ASKER

So, it seems to me that interfaces and abstracts are best used when you are writing code that other people may use and implement in their own environments.

Am I right by saying that or am I way off base?

For me, I'm an in-house developer for a company who doesn't share code with anyone. Though, I'm still trying to learn.
Right, but everybody shares code with somebody these days, even if only on the receiving end.  Microsoft shares their library code with you, and you AT LEAST need to be able to use the interfaces and abstract classes that THEY define.

Coming up with your own interfaces and abstract classes is not STRICTLY necessary all the time, it's not really something for the complete beginner, but you will probably want to do it eventually even if only to make reusing your own code easier.
For me, I'm an in-house developer for a company who doesn't share code with anyone.

Not even with the other in-house developers?
@kaufmed

There's one other developer and we work on different projects.
The original question was thoroughly addressed.

Unresolved is the question of whether the author personally should be using these constructs, and that is for the author to answer - we have given him the considerations that need to be applied for that decision.
I'd suggest a split between 35264937, 35265623, and 35267123 (all experts commenting, everything was relevant).