Thursday, January 5, 2012

What is the difference between Abstract class and Interface?

Interfaces Vs Abstract Class.

A class may implement one or more interfaces.
A class may inherit only one Abstract Class or any other class
An interface cannot have access modifiers for any types declared in it. By Default all are public.
An abstract class can contain access modifiers.
If various implementations only share method signatures then it is better to use Interfaces.
If various implementations are of the same kind and use common behavior or status then abstract class is better to use.
Requires more time to find the actual method in the corresponding classes.
All methods declared in interface must be implemented in derived class.
Only abstract methods need to be implemented in derived classes.
If we add a new method to an Interface then we have to track down all the implementations of the interface and define implementation for the new method.
If we add a new method to an abstract class then we have the option of providing default implementation and therefore all the existing code will work without any modification.
No fields can be defined in interfaces
We can define fields and constants in abstract class.