Monday, June 22, 2009

Abstract class vs. Interface

You can think of an Interface as an abstract class with all the implementation stripped out and everything that is not public removed.

-> An abstract class can have abstract members as well non abstract members. But in an interface all the members are implicitly abstract and all the members of the interface must override to its derived class.
-> Abstract classes can add more functionality without destroying the child classes that were using the old version. In an interface, creation of additional functions will have an effect on its child classes, due to the necessary implementation of interface methods to classes.
-> The members of the interface are public with no implementation. Abstract classes can have protected parts, static methods, etc.
-> A class can inherit one or more interfaces, but only one abstract class.
-> Abstract Class defines abstract member signatures which derived classes must implement. Otherwise, the derived class itself will be abstract. In Interface, compiler throws an error.
-> In Abstract Classes, we can include data fields. In Interface, we cannot include data fields. However, abstract properties may be declared.