Interface inheritance is the process of defining a new interface with all the methods found in one or more old interfaces. Unlike Object Oriented Inheritance, where all the methods(there implementations) and attributes of a base class are found in the derived class, interface inheritance only specifies that the names of the methods and attributes in the base interface is found in the derived interface. Like inheritance diagrams which describe model inheritance, lattice graphs are used to model interface inheritance. Lattice graphs look like OOP inheritance diagrams, except that the class names are replaced by interface names.
The following example (#1) demonstrates simple interface inheritance (single inheritance). In Example #1, interface IDerived is derived from the IBase interface. Notice, IBase object reference variables(IBase_ptr, IBase_var) can be used to reference all IDerived proxy objects. In C++ programming, this corresponds to base class pointer point to derived class instances.
// Example #1 : IDL Base Class
interface IBase
{
void BaseMethod();
};
// Derived Class
interface IDerived : IBase
{
void DerivedMethod( );
};
|
// Servant Code Implementation
///////////////////////////////////////
class MyServantClass:public virtual POA_IDerived
{
public:
void BaseMethod(void);
void DerivedMethod(void);
};
/////////////////////////////////////
void MyServantClass::BaseMethod(void)
{
cout<<"Base Method Called"<<endl;
}
///////////////////////////////////
void MyServantClass::DerivedMethod(void)
{
cout<<"Derived Method Called"<<endl;
}
|
// Client Code
{ IDerived_var obj;
/* Init obj code Here */
obj->BaseMethod( );
obj->DerivedMethod();
IBase_ptr base; // Base Interface Object Reference
base=obj;
base->BaseMethod( );
}
|
The Next Example (#2) demonstrates multiple interface inheritance. The interface IDerived is derived from the interfaces IBase1 and IBase2.
// Example #2 : IDL Base Class
// Base1 Class
interface IBase1
{
void Base1Method();
};
// Base2 Class
interface IBase2
{
void Base2Method();
};
// Derived Class
interface IDerived : IBase1 , IBase2
{
void DerivedMethod( );
};
|
// Servant Code Implementation
///////////////////////////////////////
class MyServantClass:public virtual POA_IDerived
{
public:
void Base1Method(void);
void Base2Method(void);
void DerivedMethod(void);
};
/////////////////////////////////////
void MyServantClass::Base1Method(void)
{
cout<<"Base1 Method Called"<<endl;
}
/////////////////////////////////////
void MyServantClass::Base2Method(void)
{
cout<<"Base2 Method Called"<<endl;
}
///////////////////////////////////
void MyServantClass::DerivedMethod(void)
{
cout<<"Derived Method Called"<<endl;
}
|
// Client Code
{ IDerived_var obj;
/* Init obj code Here */
obj->DerivedMethod();
obj->Base2Method();
obj->Base1Method();
IBase1_ptr base1=obj; // Base1 Interface
IBase2_ptr base2=obj; // Base2 Interface
base1->Base1Method();
base2->Base2Method();
}
|