Archive

Posts Tagged ‘Interfaces’

Interfaces vs. Abstract Classes

February 6, 2011 Leave a comment

There must be a million blogs on this topic, yet when the question is asked in an interview so many get it wrong or just get it partially right, or barely right at all. I have seen some good explanations and some overly complicated explanations so I want to try and get what we have summarized with enough detail so it is not overly complicated.

I think once we are aware of the fundamental differences between Interfaces and Abstract classes we will be better able to make the decision on when to use each one. Theoretically there are 5 basic differences between Interfaces and Abstract Classes:

An abstract class is a class that cannot be instantiated, but must be inherited from. An abstract class may be fully implemented, but is more usually partially implemented or not implemented at all, thereby encapsulating common functionality for inherited classes.

An interface, by contrast, is a totally abstract set of members that can be thought of as defining a contract for conduct. The implementation of an interface is left completely to the developer.

Both interfaces and abstract classes are useful for component interaction. If a method requires an interface as an argument, then any object that implements that interface can be used in the argument.

public void Spin (IWidget widget)
{

}

 

Read more…

Advertisements
Categories: Architecture Tags: ,
%d bloggers like this: