8

Possible Duplicate:
Interface vs Abstract Class (general OO)

I'm not exactly clear on the difference.

Thanks

Community
  • 1
  • 1
jmasterx
  • 52,639
  • 96
  • 311
  • 557
  • 3
    there are a lot of posts on this: http://stackoverflow.com/questions/761194/interface-vs-abstract-class-general-oo – skaz Nov 08 '10 at 18:51
  • ~Almond Joys have nuts, Mounds don't.~ Except replace "Almond Joys" with abstract classes, "Mounds" with interfaces, and "nuts" with some concrete methods. – Powerlord Nov 08 '10 at 19:06

3 Answers3

17

They are quite similar but there are some important technical differences:

  • An abstract class allows you to provide a default implementation for some of the methods but an interface does not allow you to provide any implementations.
  • You can implement multiple interfaces but you can only inherit from one abstract class.

These differences affect how the two techniques should be used:

  • You should use an interface to define a contract.
  • An abstract class can be useful to reuse code... but be aware that it is not the only way to reuse code. You should also consider other approaches such as containment.
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
4

An interface doesn't allow definition of any of the member methods, whereas an abstract class does allow some or all to be defined. A class however can only extend one class (abstract or not) but can implement as many interfaces as it wants.

Reese Moore
  • 11,524
  • 3
  • 24
  • 32
3

I like to think of an interface as a contract. any class that implements an interface, must provide details on what to do when any method defined in the contract is called. An abstract class is a class that defined a set of actual behaviors, ie more than just a contract to be implemented later, but that class can't be instantiated.

shsteimer
  • 28,436
  • 30
  • 79
  • 95