In the Dog class example, the Dog class is derived from SOMObject. Object instances of the Dog class inherit SOMObject behavior, that is, all SOMObject methods can operate on instances of the Dog class. In addition, the Dog class example defined methods not defined for SOMObject. The Dog class is a subclass of SOMObject.
LittleDogs and BigDogs, subclasses of the Dog class, can be defined. These subclasses inherit the behavior of their parent class, Dog class, as well as the behavior of their parent's parent class, SOMObject. If the Dog class had been derived from other classes that were derived from SOMObject, the new subclasses would also inherit the behavior of these ancestor classes. In addition to adding new methods to those inherited from ancestor classes, subclasses can modify or override any inherited methods.
The inheritance relationship between the new subclasses, LittleDog and BigDog and their ancestors, Dog and SOMObject, is shown in the following figure:
ÚÄÄÄÄÄÄÄÄÄÄÄ¿ ÚÄÄÄÄÄÄÄÄÄÄ¿ ÚÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ ³ ³ ³ÄÄÄÄij LittleDogs ³ ³ ³ ³ ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÙ ³ SOMObject ³ÄÄÄÄÄÄij Dogs ³ ÚÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ ³ ³ ³ÄÄÄÄij BigDogs ³ ÀÄÄÄÄÄÄÄÄÄÄÄÙ ÀÄÄÄÄÄÄÄÄÄÄÙ ÀÄÄÄÄÄÄÄÄÄÄÄÄÙ
LittleDogs and BigDogs can be differentiated by the sound, or bark, they make. For this example, instead of making an "Unknown dog noise," little dogs "Yap" and big dogs "WOOF." This means that the LittleDog and BigDog classes implement the bark method inherited from the Dog class differently. In the class definition files for the LittleDog class, this is indicated as a method override, as shown in the following sample code:
/* * Define a subclass of Dog for LittleDogs */ #ifndef ldog_idl #define ldog_idl #include <dog.idl> interface LittleDog : Dog { #ifdef __SOMIDL__ implementation { //# Class modifiers callstyle = oidl; // For compatibility with SOM1 majorversion = 1; minorversion = 2; bark: override; // Override the bark method }; #endif /* __SOMIDL__ */ }; #endif /* ldog_idl */
In the class definition files for the BigDog class, this is also indicated as a method override, as shown in the following sample code:
/* * Define a subclass of the class Dog of BigDogs */ #ifndef bdog_idl #define bdog_idl #include <dog.idl> interface BigDog : Dog { #ifdef __SOMIDL__ implementation { //# Class modifiers callstyle = oidl; // For compatibility with SOM1 majorversion = 1; minorversion = 2; bark: override; // Override the bark method }; #endif /* __SOMIDL__ */ }; #endif /* bdog_idl */
Because the parent of the LittleDog and BigDog classes is the Dog class, DOG.IDL must be included in the class definition files so that methods of the Dog class are inherited and can be referenced.
The implementations of the LittleDog and BigDog classes are similar to the implementation of the Dog class. In the implementation of the LittleDog class, as shown in the following figure, the Include file, LDOG.IH, contains the definitions for LittleDog class data, macros, and functions. The implementation of the bark method reflects the yapping of little dogs.
/* * This file was generated by the SOM compiler and Emitter Framework. * Generated using: SOM Emitter emitctm: 2.38 */ #ifndef SOM_Module_ldog_Source #define SOM_Module_ldog_Source #endif #define LittleDog_Class_Source #include "ldog.ih" /* * Override the bark method */ SOM_Scope void SOMLINK bark(LittleDog *somSelf) { /* LittleDogData *somThis = LittleDogGetData(somSelf); */ LittleDogMethodDebug("LittleDog","bark"); /* LittleDog_parent_Dog_bark(somSelf); don't call the parent method */ somPrintf("yap yap\n"); /* Do small yaps instead */ somPrintf("yap yap\n"); }
In the implementation of the BigDog class, as shown in the following sample code, the Include file, BDOG.IH, contains the definitions for BigDog class data, macros, and functions. The implementation of the bark method reflects the woofing of big dogs.
/* * This file was generated by the SOM compiler and Emitter Framework. * Generated using: SOM Emitter emitctm: 2.38 */ #ifndef SOM_Module_bdog_Source #define SOM_Module_bdog_Source #endif #define BigDog_Class_Source #include "bdog.ih" /* * Override the bark method */ SOM_Scope void SOMLINK bark(BigDog *somSelf) { int i = 0; /* Loop index */ /* BigDogData *somThis = BigDogGetData(somSelf); */ BigDogMethodDebug("BigDog","bark"); /* BigDog_parent_Dog_bark(somSelf); don't call the parent method */ for (i=0; i<4; i++) { somPrintf("WOOF WOOF\n"); /* Do woof woof instead */ } /* endfor */ }