Wednesday, January 5, 2011

Difference between abstract class and interface

1. Multiple inheritance is not possible in abstract class, but multiple inheritance is possible in interface.
2. The access modifier is public by default in interface but access modifier need not be only public in abstract class.
3. An abstract class can have a constructor but an interface doesnt have a constructor.
4. An interface can have only the signature but an abstract class can provide a default implementation
5. No fields can be defined in interface, but an abstract class can contain fields and constants

When to use an abstract class or interface

* If you are to provide a common functionality to unrelated class then use an interface
* If you plan on updating the base class throughout the life of the program its best to keep the class as abstract.
*if you are providing any default functionality then abstract class can be used.