when to use abstract class and when to use interface?
Hi friends,
I know the difference between abstract class and interface. But still i am in a confusion when to use abstract class and interface in real time. Can any one put some light on this, with any real time scenario?
Thanks for your time,
Suresh

Niradhip ChakrabortyPosted Sep 4, 2009, 4:09 AM
Abstract classes normally contain one or more abstract methods or abstract properties, such methods or properties do not provide implementations, but our derived classes must override inherited abstract methods or properties to enable obejcts ot those derived classes to be instantiated, not to override those methods or properties in derived classes is syntax error, unless the derived class also is an abstract class.
In some cases, abstract classes constitute the top few levels of the hierarchy, for Example abstract class Shape with abstract method Draw() has tow derived abstract classe Shape2D & Shape3D inherites the method Draw() & also do not provide any implementation for it. Now we have normal classes Rectangle, Square & Circle inherites from Shape2D, and another group of classes Sphere, Cylinder & Cube inherites from Shape3D. All classes at the bottom of the hierarchy must override the abstract method Draw().
A class is made abstract by declaring it with Keyword abstract.
public abstract class Shape
{
//...Class implementation
public abstract void Draw(int x, int y)
{
//this method mustn't be implemented here.
//If we do implement it, the result is a Syntax Error.
}
}
public abstract class Shape2D : Shape
{
//...Class implementation
//...you do not have to implement the the method Draw(int x, int y)
}
public class Cricle : Shape2D
{
//here we should provide an implemetation for Draw(int x, int y)
public override void Draw(int x, int y)
{
//must do some work here
}
}
Check Out my article:
http://www.c-sharpcorner.com/UploadFile/niradhip/abstractclass12307172009134526PM/abstractclass123.aspx?ArticleID=e6f143f0-e4c9-411e-994d-268d214dbe84
Interface:
If you have a dozen programmers working on different pieces of code that need to connect, it's best to define interfaces for the connections. As long as the programmers write according to the interfaces, everything will link together nicely regardless of personal choices and style.
Yet another benefit is that with interfaces you can use a class without having defined it first. For example, if you have a class that does a lot of intricate work and won't be finished before the rest of the project, the rest of the project can use an interface to it and avoid being stalled by the development of that one class.
Personally, I like the guarantees that interfaces provide. If a class implements IDisposable, I know not only that the objects can be disposed without getting compiler errors, but that they should be disposed. This keeps my code consistent and robust. You can provide similar guarantees in your code simply by adhering to an interface.
lohith bandrehalliPosted Sep 4, 2009, 4:02 AM
http://www.codeproject.com/KB/cs/jmabstractclasses.aspx
Inheritance
http://www.codersource.net/csharp_tutorial_interface.html
http://www.codeguru.com/csharp/csharp/cs_syntax/interfaces/article.php/c7563