Sunday, August 4, 2013

Abstract vs interface

  1. An abstract class is good if you think you will plan on using inheritance since it provides a common base class implementation(common methods shared between subclasses) to derived classes.
  2. An abstract class is also good if you want to be able to declare non-public members. In an interface, all methods must be public.
  3. If you think you will need to add methods in the future, then an abstract class is a better choice. Because if you add new method headings to an interface, then all of the classes that already implement that interface will have to be changed to implement the new methods. That can be quite a hassle.
  4. Interfaces are a good choice when you think that the API will not change for a while.
  5. Interfaces are also good when you want to have something similar to multiple inheritance, since you can implement multiple interfaces.