Sunday, October 24, 2010

Difference Between Abstract Class and Interface

What is an Abstract Class?
An abstract class is a special kind of class that cannot be instantiated. So the question is why we need a class that cannot be instantiated? An abstract class is only to be sub-classed (inherited from). In other words, it only allows other classes to inherit from it but cannot be instantiated. The advantage is that it enforces certain hierarchies for all the subclasses. In simple words, it is a kind of contract that forces all the subclasses to carry on the same hierarchies or standards.

What is an Interface?
An interface is not a class. It is an entity that is defined by the word Interface. An interface has no implementation; it only has the signature or in other words, just the definition of the methods without the body. As one of the similarities to Abstract class, it is a contract that is used to define hierarchies for all subclasses or it defines specific set of methods and their arguments. The main difference between them is that a class can implement more than one interface but can only inherit from one abstract class. Since C# doesn�t support multiple inheritance, interfaces are used to implement multiple inheritance.

1)An Abstract Class can contain default Implementation(some non abstract methods), where as an Interface should not contain any implementation at all.When a class inherits from an abstract, the derived class must implement all the abstract methods declared in the base class. an abstract class can inherit from another non-abstract class.

2)There is no difference in the functionality of these two.The only difference is that a class cannot extend an abstract class if it already is extending some other class.An interface on the other hand can be implemented in any situation which makes them very powerful.Also user defined exceptions can be defined within an interface itself which is not the case with an abstract class.

3)Abstract class contatins one or more abstract methods. where Interface contains all abstract methods and final declarations.

4)Abstract classes are useful in a situation that Some general methods should be implemented and specialization behaviour should be implemented by child classes. Interafaces are useful in a situation that all properties should be implemented we can use this scenario

5)Interface must always have by default public and abstract methods while abstract class can have non abstract methods.

6)All the variables in interface are by default public static constant while in abstract class you may have instant variables.

7)Interfaces are slow as it requires extra indirection to find corresponding method in the actual class. Abstract classes are fast.

8)Accessibility modifier(Public/Private/internal) is allowed for abstract class. Interface doesn't allow accessibility modifier.

9)Interface come in inheritance chain but abstract class does not come. (Please test not confirmed.)

10)An abstract class can contain fields, constructors, or destructors and implement properties. An interface can not contain fields, constructors, or destructors and it has only the property's signature but no implementation.

11)You can prevent not required method to override in Abstract class, but not in Interface.

12)Abstract classes can inherit interfaces while interfaces cannot inherit abstract classes.

13)An abstract class may or may not have abstract method,but it is useless without abstract method,so an abstract class should have atleast single abstract method. While in case of an interface it is hardcode that an interface can have only the abstract methods with it.

14)We can't create object of abstract class.

15)We can't make method body with abstract method.

16)Abstract classes can inherit interfaces while interfaces cannot inherit abstract classes And abstract class can implement more than one interface.

Which is better interface or abstract class?
Obvious abstract class is better as it is fast in comparison of interface. And you can define your method also here.We use interface only in compel where we can not use abstract class we use interface[In case if class is already inherited with some other class like form.].