Click here to Skip to main content
14,981,116 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
If we can also define same things in normal class then why Abstract class and interface?
Posted
Updated 6-Feb-11 22:45pm

An abstract class can have some methods implemented while an interface is per se abstract in that it doesn't have any methods or properties implemented but only defined.

Still have questions? Leave a comment then.

Read here: http://msdn.microsoft.com/en-us/library/ms173156.aspx[^]
and here: http://en.csharp-online.net/Interfaces_and_Abstract_Classes[^]

Cheers!
   
Comments
Espen Harlinn 7-Feb-11 15:14pm
   
Good answer :)
   
Comments
Manfred Rudolf Bihy 7-Feb-11 7:04am
   
Good links. 5+
CP rules!
TweakBird 7-Feb-11 7:10am
   
Thank you Manfred R.Bihy
Because sometimes there's no need for a class, i.e. you don't need to provide any functionality: you just need to specify a contract (the interface) between you and the folks that will implement the interface.
:)
   
Both have their use and it is up to you to decide when to use which.

I use Interfaces practically all the time to define the contracts my componets will support or will give support to. Sometimes I myself implement those interfaces, sometimes it is somebody else. When is the case I create one possible, base and generic implementation of the interface. This is good when you want to give some logic ready to your consumers, this make it easier to distribute flexible frameworks and, since you still implement the interfaces there is always the possibility of doing it all from scratch and or replace parts of the pipe.

Interfaces also are a great deal of help when unit testing you componets, you dont need to have all of your dependencies ready when you develop, having the interface you can mock those dependencies (using Mocking Framewors) and test your component without any problems.

The more you become a professional developer, more you will see the differences between abstracs and interfaces. For now, I hope I helped giving you an entry point for you to find out when and where to use them for yourself.
   
Comments
Manfred Rudolf Bihy 7-Feb-11 7:14am
   
Good answer! 5+
I especially liked the last paragraph. :)
Espen Harlinn 7-Feb-11 15:15pm
   
Good answer! a 5

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




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900