:: :: ::

Archive for January, 2007

Understanding Interface

Monday, January 8th, 2007
According to definition, an Interface in a system is considered as a contract. Classes that implement the contract must adhere to its terms and conditions. It’s like a legal agreement where contractee (classes) which are bound by the contract and hence must implement each and every aspect of that exactly as it is outlined or defined.  From the implementation viewpoint this definition or perspective is good, however so far as designing is concern it doesn’t help much. It’s very difficult for developer from a non object oriented programming background, to understand why has been a particular interface designed that way. They very well understand how to implement it, however can’t think about incorporating ones own interface into application design. To prove that I asked following questions to many of such developers. And got expected answers.
  1. Q: Do you know the interface concept in contemporary languages like C#, VB.Net or Java? A: Yes
  2. Q: What is an interface? A: Interface is some thing, which only contains declaration of methods, properties. A class that implements an interface must implement it exactly as it is defined.
  3. Q: Why can an interface only contain structure, not concrete code? « No Answer »
  4. Q: Have you implemented interfaces in your projects? A: Yes.
  5. Q: Have you designed interfaces in your project? A: No, it was never required.

See the answer to question number 3: most of the developer doesn’t even know why an interface can not contain concrete code. If you are among such developer, don’t worry, you will understand it why. Question number 5 reveals that many developers in reality never ever create a single interface in their software development career on their own. In other words it is non-utilization of most important feature of the modern languages. Changing Perspective Let me ask you few questions which will eventually help in understanding of interfaces. You may find them silly and weird! But please try to answer them for a moment.

  1. Why don’t you call a car, a bike?
  2. What is the reason that, you do not consider a monkey as an elephant?
  3. Why do you think that fish can not fly?

You very well know the answers to these questions, right? Questions are different but answer is common, that every object questioned here has some characteristics, qualities and properties. They have their way of performing some action. For example: We can not call a car a bike because we know the structure of the bike somewhere in our mind, unlike cars; bikes are motor vehicle with two wheels and instead of driven, it’s ridden. We can not consider a monkey as an elephant because we know that monkey has different qualities and attributes then those of elephant. Same way we know that fish can not fly because fish is an aquatic animal which characteristically having fins, gills, and a streamlined body. Fish can’t have organs to fly. Flying is a quality of birds. I am repeating this again; every object has some quality, attributes and characteristics. They have their own way to operate something, perform actions and they know how to communicate and interact with other objects. Combining all these attitude, attributes, qualities, characteristics, communicating and operating way et cetera defines an object. And definition or classification of object is an interface.  Please notice, I have used two words to define an interface: Definition and Classification.  Lets consider following example.

ICar 
– Toyota Corolla
– Mercedes Benz

Here Toyota Corolla and Mercedes Benz both implements an interface called ICar. So by considering interface as a definition: I can say Toyota Corolla and Mercedes Benz are ICar and they have all the attribute and qualities which fulfills the requirements to be an ICar. By considering interface as a classification I can say: ICar is a category. Toyota Corolla and Mercedes falls into category of an ICar because they satisfy all the characteristics and operational methods required to be considered as an ICar. Other way round, if any of the ICar, Toyota Corolla or Mercedes Benz would not be considered as ICar if they had not satisfied requirements to become an ICar. Non technically manufacturer of these ICar must put all the attributes and features in their respective cars to be considered or classified as an ICar. Technically developer of class ToyotaCorolla and MercedesBenz must implement an interface ICar to be considered these class as an ICar, failing to do so will result in compile time error. I hope you not have got the basic understanding interface as what exactly it is’ Next article will put some more highlight on interfaces.