Inherit the Database: Oracle9i's Support for Object Type Inheritance
Pages: 1, 2, 3, 4, 5
Features of Inheritance in Oracle9i
Here are some of the ways you can take advantage of inheritance with object types:
A subtype can be defined from a supertype either directly or indirectly, through multiple levels of other subtypes.
Oracle supports only single inheritance, which means that a subtype can be derived only from a single supertype. A supertype can have more than one sibling subtype (all subtypes of the same supertype), but a subtype can have no more than one direct parent supertype.
Subtypes inherit all attributes and members from all of their supertypes.
Subtypes can define new attributes and methods. These new elements can be in addition to the inherited ones. They can also override or replace existing methods.
When you call an object-type method, PL/SQL automatically executes the appropriate version of the method, based on the type of the object. This is known as dynamic dispatch or dynamic polymorphism.
You can define an object type (and even an individual member in an object type) to be
FINAL, which means that you can't define a subtype from that type (override the member in a subtype). If an object type isNOT FINAL, it can be a supertype of another object type.You can define an object type as
NOT INSTANTIABLE, which means that you won't be able to instantiate objects from the type. Such an object type can only act as a supertype for other types.You can declare an individual method as
NOT INSTANTIABLE, which means that it exists only as a "template" for subtype implementations. You specify, in other words, only the header of the method without providing an implementation.
There are many other nuances in the ways that you can define and use object types, but these concepts will be enough for a single article.
Defining an Object Hierarchy in PL/SQL
Let's take a look at how you set up a hierarchy with Oracle object types. Here's my definition of the root living thing entity:
CREATE OR REPLACE TYPE living_thing_ot
IS OBJECT (
species VARCHAR2 (100),
NOT INSTANTIABLE MEMBER
PROCEDURE showpoliticalpower
)
NOT INSTANTIABLE
NOT FINAL;
/
All of my other objects are subtypes of this single object type. It's made up of a single
attribute, species, and a single member, the showpoliticalpower method. Here are some of
the special characteristics of this object type:
It's
NOT INSTANTIABLE, which means that I can't declare an object based on this type. If I try to, as shown here, I'll get an error:/* Instantiate the un-instantiable */ DECLARE squirrel living_thing_ot := living_thing_ot (); BEGIN IF squirrel.dob < SYSDATE THEN DBMS_OUTPUT.put_line ( 'Senior citizen' ); END IF; END; / ERROR: PLS-00713: attempting to instantiate a type that is NOT INSTANTIABLEThe
living_thing_otobject type can only be used as the starting point for other object types.It's
NOT FINAL, which means that I can declare subtypes from the object type. Note that the combination ofNOT INSTANTIABLEandFINALwould result in a thoroughly useless object type.The
showpoliticalpowermethod is defined asNOT INSTANTIABLE. This means that I won't provide an implementation of this procedure in theliving_thing_ottype (There is, in fact, no body forliving_thing_ot). Instead, I'm requiring that any object type declared as a subtype ofliving_thing_otmust provide its own implementation of the procedure. In Java, this is called an abstract method, andliving_thing_otwould be called an abstract class. I like that terminology and will use it in the rest of this article.
So let's see how I might take advantage of the living_thing_ot object type. Listing 1
shows the specification for the person type.
Listing 1. Specification of the person type.
CREATE OR REPLACE TYPE person_ot UNDER living_thing_ot
(
name VARCHAR2 (100),
weight NUMBER,
dob TIMESTAMP (3),
-- New methods in object type
MEMBER PROCEDURE show,
MEMBER FUNCTION age RETURN INTERVAL YEAR TO MONTH,
FINAL MEMBER PROCEDURE when_crime_committed,
MEMBER PROCEDURE showpunishment,
-- Provide overriding implementation of abstract method.
OVERRIDING MEMBER PROCEDURE showpoliticalpower
)
INSTANTIABLE
NOT FINAL;
/
Some observations about the person type:
Instead of specifying the type
AS OBJECT, I instead use theUNDERclause to indicate thatperson_otis a subtype ofliving_thing_ot.Any object declared from this type has a total of four attributes: the species attribute from
living_thing_ot, plus the three attributes of person (name, weight, and DOB- date of birth).This type adds four new methods (
show,age,when_crime_committed, andshowpunishment) and also overrides the abstract (unimplemented) method of the same name inliving_thing_ot.The person type is declared as
INSTANTIABLEandNOT FINAL. This means that I can declare objects based on this object type and I can also define subtypes ofperson_ot.The
when_crime_committedmethod is declared asFINAL. This means that if I define a subtype ofperson_ot, I'm NOT allowed to overridewhen_crime_committedwith a new, more specific definition. If I try to do so, I will raise an exception as shown here:CREATE OR REPLACE TYPE immigrant_ot under person_ot ( from_nation VARCHAR2(200), OVERRIDING MEMBER PROCEDURE when_crime_committed ) ; / ERROR: PLS-00637: FINAL method cannot be overridden or hiddenBy marking a method as
FINAL, I make sure that whenever that method is executed for an object in the hierarchy, it's always executing the code in which it was finalized.The
FINALstatus of an object type can be modified with theALTER TYPEstatement. Suppose, for example, I want to change the company type fromFINALtoNOT FINAL. I can issue this statement:ALTER TYPE company_ot NOT FINAL;You can also change a type from
NOT FINALtoFINALwith theALTER TYPEcommand, but only if the target type has no subtypes.



