Thursday, March 1, 2007

Understanding: Interfaces

When creating applications, there sometimes comes the need to use interfaces. An 'interface' is one of the types available in the VB .Net language (and for that matter almost all other OO languages). Interfaces are perhaps one of the most difficult types to explain. Most writeups or articles explain it as a contract; a contract that binds two classes together.

I, on the other hand, prefer to say: Interfaces can be best explained as a 'language'; like English or Arabic. Sometimes classes need to communicate between each other, if written by the same person, that would not be much of a problem. But sometimes these classes are written by a number of different people, probably people from another firm, country and/or continent.

In order to communicate with each other, like us humans (at least those of us who call themselves that :-)) classes need a common language they all speak. This language must be understood by all and follow the same rules and regulations everywhere.

In programming this is called an interface. It is a way of ensuring all related classes speak the same language. For example, if class A wants class B to do a certain task, the only way for class A to insure that class B can do the task, is by checking if class B implements the ICanDoTask (for example) interface.

Unlike inheritance, a class can implement an unlimited number of interfaces.

Perhaps you can understand more if you read this:


No comments: