OOPS: interface vs abstract

srivmanish007
srivmanish007 used Ask the Experts™
on
Exactly what could be the possible scenarions when we should make interface and when we shold go for abstract classes?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
More test questions.

Author

Commented:
I was expecting a solution from your side but i  thing it is waste to ask questions because no one is giving proper answers and sending to the sites'url which is not a better way.
I have queried about 5 questions but i have not got any one's answer just a got a story for all the questions.........
It's waste to effort the time as well as money on  this site to  get answers ithought that most  probably this site willl be helpful for  me but no ..................
Top Expert 2009
Commented:
Hi srivmanish007,

Since you've received no good answers I'll try to help.

There are two key differences between a class and an interface.

1) Classes require hierarchy, and so sub-classes must belong to that hierarchy. interfaces do not require hierarchy and can be mixed in as needed. In the case of languages that do not allow multiple inheritance, then interfaces provide the alternative way to mix traits from multiple, unrelated ancestors, to a point, where you can only inherit from a single class at a time.

2) Interfaces do not provide implementation. Classes may. An abstract base class can define some default functionality, for example, some common variables or methods. An interface, however, is just a signature of methods that must be implemented.

Rule of thumb, when factoring out common behavior in a class hierarchy, use a class. When creating behaviour that may be used across unrelated classes, use an interface.

A class means, it is a duck, because its mother was a duck.
Interface means, if it quacks, it acts like a duck, so we treat it like one. A duck could also cluck like a chicken, by implementing the chicken interface as well.

Hope that helps.
anarki_jimbelSenior Developer

Commented:
There are some good articles on internet about interfaces and abstract classes aimed on beginners. Please find some:

http://www.codeproject.com/KB/cs/abstractsvsinterfaces.aspx

and

http://en.csharp-online.net/Should_I_use_an_abstract_class_or_an_interface%3F
http://geekswithblogs.net/mahesh/archive/2006/07/05/84120.aspx

The first one has some code samples useful for understanding. Please read it - it's a good one.

Author

Commented:
no comments

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial