Wednesday, March 19, 2008

Difference between abstract class and interface

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.
Moreover..
1. Abstract classes can have concrete method while interfaces have no methods implemented.
2. Interface do not come in inheriting chain, while abstract class come in inheritance.
3. Abstract classes are faster than interfaces.
4. By default, all members in Interface is Public whereas members in Abstract class may have different.

3 comments:

Olivo Miotto said...

Actually, the statements made are not really correct.

In terms of functionality, only classes contain *actual* functionality. Interfaces are only contracts requiring functionality to be provided.

I think the best way of thinking of an abstract class is that it is class that partially implements an interface. One or more methods are still to be implemented, but there is code to be reused. Interfaces provide no code.

>Abstract classes are faster than interfaces.

That's a strange concept. Where does it come from?

>By default, all members in Interface is Public whereas members in Abstract class may have different.

That's not necessarily true.

SYSTEM64.DLL said...

>Abstract classes are faster than interfaces.

Performance point of view,
Complier requires more time to find the actual method in the corresponding classes that extend from Interface.

Design point of view,
It's nothing.

> By default, all members in Interface is Public whereas members in Abstract class may have different.

That isn't true.

Anonymous said...

Most people confuse the abstract class and class interface.Abstract are used only when there is "is-a" type of relationship between classes.Interfaces can be implemented by classes that are not related.
holidays to paphos cyprus