Inheritance and type substitutability

In a type hierarchy, a subtype automatically inherits all the routines defined on its supertype. Consequently, if you call a routine with an argument of a subtype and no routines are defined on the subtype, the database server can invoke a routine that is defined on a supertype. Type substitutability refers to the ability to use an instance of a subtype when an instance of a supertype is expected. As an example, suppose that you create a routine p_info() that accepts an argument of type person_t and returns the last name and birthdate of an instance of type person_t. If no other p_info() routines are registered, and you invoke p_info() with an argument of type employee_t, the routine returns the name and birthdate fields (inherited from person_t) from an instance of type employee_t. This behavior is possible because employee_t inherits the functions of its supertype, person_t.

In general, when the database server attempts to evaluate a routine, the database server searches for a signature that matches the routine name and the arguments that you specify when you invoke the routine. If such a routine is found, then the database server uses this routine. If an exact match is not found, the database server attempts to find a routine with the same name and whose argument type is a supertype of the argument type that is specified when the routine is invoked. For example, suppose that the database server searches for a routine that it can use when a get() routine is called with an argument of the subtype sales_rep_t. Although no get() routine has been defined on the sales_rep_t type, the database server searches for a routine until it finds a get() routine that has been defined on a supertype in the hierarchy. In this case, neither sales_rep_t nor its supertype employee_t has a get() routine defined over it. However, because a routine is defined for person_t, this routine is invoked to operate on an instance of sales_rep_t.
Figure 1: Example of how the database server searches for a routine in a type hierarchy

begin figure description - This figure is described in the surrounding text. - end figure description

The process in which the database server searches for a routine that it can use is called routine resolution. For more information about routine resolution, see HCL OneDB™ User-Defined Routines and Data Types Developer's Guide.