0

Possible Duplicates:
interface and abstract class.
Abstract classes vs Interfaces

Can anyone tell me when one should use an abstract class and when one should use an interface with sample scenarios?

Community
  • 1
  • 1
Thomas
  • 33,544
  • 126
  • 357
  • 626

3 Answers3

3

You should use an abstract class if you need to supply a base implementation.

Otherwise, use an interface.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
3

If you're defining behavior (like IEnumerable, since a List acts like an IEnumerable), then use an interface, but if you're defining a kind of object (an IS-A) relationship, use an abstract class (like Stream -- since a MemoryStream IS-A Stream).

user541686
  • 205,094
  • 128
  • 528
  • 886
2

When you want to provide an implementation of a method/set of methods but don't want people to directly instantiate your parent class, use an Abstract Class.

If you want to define what members a class must interface, but not provide any implementation, use an Interface.

Justin Niessner
  • 242,243
  • 40
  • 408
  • 536