8.3 Creating Object Types

Table of Contents Previous Next


8 Object Types and Objects : 8.3 Creating Object Types

You can use the CREATE TYPE command to create an object type specification, and the CREATE TYPE BODY command to create an object type body. This section provides some examples using the CREATE TYPE and CREATE TYPE BODY commands.
The first example creates the addr_object_type object type that contains only attributes and no methods:
Object type emp_obj_typ contains a member method named display_emp. display_emp uses a SELF parameter, that passes the object instance on which the method is invoked.
A SELF parameter is a parameter whose data type is that of the object type being defined. SELF always refers to the instance that is invoking the method. A SELF parameter is the first parameter in a member procedure or function regardless of whether it is explicitly declared in the parameter list.
You can also use the SELF parameter in an object type body. To illustrate how the SELF parameter would be used in the CREATE TYPE BODY command, the preceding object type body could be written as follows:
Both versions of the emp_obj_typ body are completely equivalent.
Like a member method, a static method belongs to a type. A static method, however, is invoked not by an instance of the type, but by using the name of the type. For example, to invoke a static function named get_count, defined within the emp_obj_type type, you would write:
A static method does not have access to, and cannot change the attributes of an object instance, and does not typically work with an instance of the type.
The following object type specification includes a static function get_dname and a member procedure display_dept:
The object type body for dept_obj_typ defines a static function named get_dname and a member procedure named display_dept:
Within the static function get_dname, there can be no references to SELF. Since a static function is invoked independently of any object instance, it has no implicit access to any object attribute.
Member procedure display_dept can access the deptno attribute of the object instance passed in the SELF parameter. It is not necessary to explicitly declare the SELF parameter in the display_dept parameter list.
The last DBMS_OUTPUT.PUT_LINE statement in the display_dept procedure includes a call to the static function get_dname (qualified by its object type name dept_obj_typ).
For example, if you define a type named address, each constructor is named address. You may overload a constructor by creating one or more different constructor functions with the same name, but with different argument types.
To create a custom constructor, declare the constructor function (using the keyword constructor) in the CREATE TYPE command and define the construction function in the CREATE TYPE BODY command. For example, you may wish to create a custom constructor for the address type which computes the city and state given a street_address and postal_code:
Custom constructor functions are also used to enforce business rules that restrict the state of an object. For example, if you define an object type to represent a payment, you can use a custom constructor to ensure that no object of type payment can be created with an amount that is NULL, negative, or zero. The default constructor would set payment.amount to NULL so you must create a custom constructor (whose signature matches the default constructor) to prohibit NULL amounts.

8 Object Types and Objects : 8.3 Creating Object Types

Table of Contents Previous Next