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.

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 same abstract class also may come in inheritance chain.

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.
17) An abstract class can have delegate while an interface can not.

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.].

Why interface is slow in comparison of abstract class?
In Java's early days, there was a technical reason for the difference. Do you know what a "virtual table" or "vtbl" is? Basically, a simple vtbl could be used for directly inherited methods, and so the JVM just had to look at two pointers to find the code for a method inherited from an abstract class -- that's relatively fast. But since any class can implement any number of interfaces, finding the code that implements an interface method would involve following a pointer to a table of interfaces implemented by a class, then searching through the table to find a vtbl for the implementation of that interface, and then finding the method pointer in the table. That's obviously a lot more work, so calling an interface method used to be measurably slower.

These days, JVMs are a lot smarter, and most of this sort of lookup is done during dynamic compilation, so the runtime differences are small or nonexistent.