O'Reilly Databases

oreilly.comSafari Books Online.Conferences.

We've expanded our coverage and improved our search! Search for all things Database across O'Reilly!

Search Search Tips

advertisement
AddThis Social Bookmark Button

Listen Print Subscribe to Databases Subscribe to Newsletters

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 is NOT 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 
    INSTANTIABLE

    The living_thing_ot object 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 of NOT INSTANTIABLE and FINAL would result in a thoroughly useless object type.

  • The showpoliticalpower method is defined as NOT INSTANTIABLE. This means that I won't provide an implementation of this procedure in the living_thing_ot type (There is, in fact, no body for living_thing_ot). Instead, I'm requiring that any object type declared as a subtype of living_thing_ot must provide its own implementation of the procedure. In Java, this is called an abstract method, and living_thing_ot would 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 the UNDER clause to indicate that person_ot is a subtype of living_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, and showpunishment) and also overrides the abstract (unimplemented) method of the same name in living_thing_ot.

  • The person type is declared as INSTANTIABLE and NOT FINAL. This means that I can declare objects based on this object type and I can also define subtypes of person_ot.

  • The when_crime_committed method is declared as FINAL. This means that if I define a subtype of person_ot, I'm NOT allowed to override when_crime_committed with 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 hidden

    By 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 FINAL status of an object type can be modified with the ALTER TYPE statement. Suppose, for example, I want to change the company type from FINAL to NOT FINAL. I can issue this statement:

    ALTER TYPE company_ot NOT FINAL;

    You can also change a type from NOT FINAL to FINAL with the ALTER TYPE command, but only if the target type has no subtypes.

Pages: 1, 2, 3, 4, 5

Next Pagearrow




Tagged Articles

Be the first to post this article to del.icio.us

Sponsored Resources

  • Inside Lightroom

Related to this Article

Understanding Oracle Clinical Understanding Oracle Clinical
by Joan M. Johnson
June 2009
$9.99 USD

New Features in Oracle 9i New Features in Oracle 9i
by Howard J. Rogers
June 2009
$5.95 USD

Advertisement
O'Reilly Media

©2009, O'Reilly Media, Inc.
(707) 827-7000 / (800) 998-9938
All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.
About O'Reilly
Academic Solutions
Authors
Contacts
Customer Service
Jobs
Newsletters
O'Reilly Labs
Press Room
Privacy Policy
RSS Feeds
Terms of Service
User Groups
Writing for O'Reilly
Content Archive
Business Technology
Computer Technology
Google
Microsoft
Mobile
Network
Operating System
Digital Photography
Programming
Software
Web
Web Design
More O'Reilly Sites
O'Reilly Radar
Ignite
Tools of Change for Publishing
Digital Media
Inside iPhone
makezine.com
craftzine.com
hackszine.com
perl.com
xml.com

Partner Sites
InsideRIA
java.net
O'Reilly Insights on Forbes.com