Thursday, July 9, 2009

The importance of Interface classes

I am sure you might have heard about interfaces. But then why do you need them. You anyways are going to implement those classes. Why take the unnecessary overhead of writing interfaces for them as well.

Actually its not necessary to write a interface class for all classes that you write. But there are certain cases when its much much better if you have them in place.

First what are interface classes. Well this is a base class which has pure declaration of all the public methods need in the target class. All these methods are make pure virtual functions. No implementation is added to this class. For example if you write a class Time, you would want to declare an interface named, ITime. So the class declarations would be as follows.

class ITime 
{
      public : int getHour() = 0;
                   int getMinute()= 0;
                   int getSecond()= 0;
                   void setHour(int hour)= 0;
                   void setMinute(int min)= 0;
                   void setSecond(int sec)= 0;
};
class Time: public ITime
{
      public : int getHour() = 0;
                   int getMinute()= 0;
                   int getSecond()= 0;
                   void setHour(int hour)= 0;
                   void setMinute(int min)= 0;
                   void setSecond(int sec)= 0;
      private: int m_hour, m_min, m_sec;
};

The interface class has no implementation but the concrete class does. Concrete class contains all additional private member variables needed as well as constructors and destructors, as needed.

When do you need it? For classes which are instantiated at a few locations but used at a lot of other places, it makes sense to create interfaces for them.

But why do all this circus. Well the reason is pretty straight forward, to allow users of this class not care about whatever is there in the actual implementation. All internally used methods and variables are not known to the user of this class. Also since the concrete class headers are not included at all places where this class is being used, changes in internal implementation need not lead to compilation of each of the other classes. Only if something is changed in Interface class, recompilation would take place in all places else its a faster process.

This is more or less what I understand as a need for Interfaces. Please do let me know if you think otherwise.

2 comments:

st203 said...

Derived class do not have to implement the pure virtual methods of their parent classes.

They can simply make them pure virtual methods again.

This is not really a sound contract, whereas with true interfaces, a body of code has to be provided or there is no compilation.

100rabh™ said...

Yeah you are right, example is probably not meant as an example of creating interfaces, but I just wanted to convey the use of Interface classes.

Glad you noticed :-)

Actualy implimentation should actually be something like this
class Time: public ITime
{
public : int getHour(){return m_hour;}
int getMinute(){return m_min;}
int getSecond(){return m_sec;}
void setHour(int hour){m_hour = hour;}
void setMinute(int min){m_min = min;}
void setSecond(int sec){ m_sec = sec;}
private: int m_hour, m_min, m_sec;
};