Interface and abstract clarification

aspnetDottech
aspnetDottech used Ask the Experts™
on
real time example of interface and abstract class and when to use interface or abstract class
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
I hope you will get what you want:

An interface defines a contract - something anything implementing that interface MUST do. It can NEVER specify how that something is done.

An abstract class is similar in that it also defines what must be done by a class implementing it, but it can have some implementation as well. In many cases, these implementations are considered as default implementations and can be overridden in derived classes. If derived classes don't ovverride it, than the abstract class's implementation is used. If you want something that can be used in all derived classes, you can put it in the base class.

Although they both provide a contract of sorts, they are different. A class can implement many intrerfaces, but can only have a single base class. Using an interface, when you publish it, it's pretty much sealed. Any change in the interface will require you to change every implementation of that interface. If you publish the interface to many developers, that's not really an option. Using an abstract base class, you can add some behaviour / data to the base class and if done properly, it won't need any changes in code for classes deriving from the base class. All existing code will exist to work. But this takes away the single base class a class can derive from and enforces the "evil" of inheritance.

Think of an interface in terms of behaviour and abstract base classes in terms of implementation.

Reference articles:
http://forums.asp.net/t/1523088.aspx
http://www.codeproject.com/Questions/43970/Real-world-examples-of-abstract-classes-and-interf.aspx

Author

Commented:


Thanks, i got to know the details of interface and abstract. however, I would like to know the real time projects usage apart from animals, vechile and general examples available on internet.
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

AndyAinscowFreelance programmer / Consultant

Commented:
>>I would like to know the real time projects usage apart from animals, vechile and general examples available on internet.


Does it really help if I said XXX corporation uses both techniques in their app YYY  (which could involve vehicles unfortunately so any details would be rejected by your clause).  The main point is that both do have their uses.
The above example use the employee class usage.

Author

Commented:
can you please help me with small expample here only
Hi aspnetDottech

I request you to go through the code from the reference i wrote above, it's really easy and powerful to understand. It's not easy to show the difference them in a few words, but i am sure that this example can make you satisfied.

Trust me and go though once!
If you dont understand anything from the code, let me know.
URL: http://www.codeproject.com/KB/cs/abstractsvsinterfaces.aspx

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