0% found this document useful (0 votes)
6 views11 pages

Inheritance SQL

inheritance SQL
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views11 pages

Inheritance SQL

inheritance SQL
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

2.

3 Inheritance in SQL Object Types


SQL object inheritance is based on a family tree of object types that forms
a type hierarchy. The type hierarchy consists of a parent object type,
called a supertype, and one or more levels of child object types, called
subtypes, which are derived from the parent.

Topics:

 About Inheritance in SQL Object Types


 Supertypes and Subtypes
 FINAL and NOT FINAL Types and Methods for Inheritance
 Subtype Creation
 NOT INSTANTIABLE Types and Methods
 Overloaded and Overridden Methods
 Dynamic Method Dispatch
 Type Substitution in a Type Hierarchy
 Column and Row Substitutability
 Newly Created Subtypes Stored in Substitutable Columns
 Dropping Subtypes After Creating Substitutable Columns
 Turning Off Substitutability in a New Table
 Constraining Substitutability
 Modifying Substitutability on a Table
 Restrictions on Modifying Substitutability
 Assignments Across Types
2.3.1 About Inheritance in SQL Object Types
Inheritance is the mechanism that connects subtypes in a hierarchy to
their supertypes.

Subtypes automatically inherit the attributes and methods of their parent


type. Also, the inheritance link remains alive. Subtypes automatically
acquire any changes made to these attributes or methods in the parent:
any attributes or methods updated in a supertype are updated in
subtypes as well.

Note:
Oracle only supports single inheritance. Therefore, a subtype can derive
directly from only one supertype, not more than one.
With object types in a type hierarchy, you can model an entity such as a
customer, and also define different specializing subtypes of customers
under the original type. You can then perform operations on a hierarchy
and have each type implement and execute the operation in a special
way.
2.3.2 Supertypes and Subtypes
A subtype can be derived from a supertype either directly or indirectly
through intervening levels of other subtypes.
A supertype can have multiple sibling subtypes, but a subtype can have at
most one direct parent supertype (single inheritance).

Figure 2-1 Supertypes and Subtypes in Type Hierarchy

Description of "Figure 2-1 Supertypes and Subtypes in Type Hierarchy"


To derive a subtype from a supertype, define a specialized variant of the
supertype that adds new attributes and methods to the set inherited from
the parent or redefine (override) the inherited methods. For example,
from a person_typ object type you might derive the specialized
types student_typ and employee_typ. Each of these subtypes is still
a person_typ, but a special kind of person. What distinguishes a subtype
from its parent supertype is some change made to the attributes or
methods that the subtype received from its parent.
Unless a subtype redefines an inherited method, it always contains the
same core set of attributes and methods that are in the parent type, plus
any attributes and methods that it adds. If a person_typ object type has the
three attributes idno, name, and phone and the method get_idno(), then any
object type that is derived from person_typ will have these same three
attributes and a method get_idno(). If the definition of person_typ changes, so
do the definitions of any subtypes.
Subtypes are created using the keyword UNDER as follows:
CREATE TYPE student_typ UNDER person_typ
You can specialize the attributes or methods of a subtype in these ways:

 Add new attributes that its parent supertype does not have.
For example, you might specialize student_typ as a special kind
of person_typ by adding an attribute for major. A subtype cannot drop
or change the type of an attribute it inherited from its parent; it can
only add new attributes.
 Add entirely new methods that the parent does not have.
 Change the implementation of some of the methods that a subtype
inherits so that the subtype's version executes different code from
the parent's.
For example, a ellipse object might define a method calculate(). Two
subtypes of ellipse_typ, circle_typ and sphere_typ, might each implement
this method in a different way.

The inheritance relationship between a supertype and its subtypes is the


source of much of the power of objects and much of their complexity.

Being able to change a method in a supertype and have the change take
effect in all the subtypes downstream just by recompiling is very powerful.
But this same capability means that you have to consider whether or not
you want to allow a type to be specialized or a method to be redefined.
Similarly, for a table or column to be able to contain any type in a
hierarchy is also powerful, but you must decide whether or not to allow
this in a particular case. Also, you may need to constrain DML statements
and queries so that they pick out just the range of types that you want
from the type hierarchy.
See Also:
 See Example 2-15 for a complete example
 "Overloaded and Overridden Methods"

2.3.3 FINAL and NOT FINAL Types and Methods


for Inheritance
Object types can be inheritable and methods can be overridden if they are
so defined.

For an object type or method to be inheritable, the definition must specify


that it is inheritable. For both types and methods, the
keywords FINAL or NOT FINAL are used are used to determine inheritability.
 Object type: For an object type to be inheritable, thus allowing
subtypes to be derived from it, the object definition must specify
this.
NOT FINAL means subtypes can be derived. FINAL, (default) means
that no subtypes can be derived from it.
 Method: The definition must indicate whether or not it can be
overridden.
NOT FINAL(default) means the method can be
overridden. FINAL means that subtypes cannot override it by
providing their own implementation.
See Also:
 Example 2-13
 Example 2-12
 Changing a FINAL TYPE to NOT FINAL

2.3.3.1 Creating an Object Type as NOT FINAL with a


FINAL Member Function
You can create a NOT FINAL object type with a FINAL member function as
in Example 2-12.

Example 2-12 Creating an Object Type as NOT FINAL with a FINAL Member
Function

Copy
DROP TYPE person_typ FORCE;
-- above necessary if you have previously created object

CREATE OR REPLACE TYPE person_typ AS OBJECT (


idno NUMBER,
name VARCHAR2(30),
phone VARCHAR2(20),
FINAL MAP MEMBER FUNCTION get_idno RETURN NUMBER)
NOT FINAL;
/

2.3.3.2 Creating a NOT FINAL Object Type


You can create an object type as NOT FINAL.
Example 2-13 declares person_typ to be a NOT FINAL type and therefore
subtypes of person_typ can be defined.

Example 2-13 Creating the person_typ Object Type as NOT FINAL

Copy
DROP TYPE person_typ FORCE;
-- above necessary if you have previously created object

CREATE OR REPLACE TYPE person_typ AS OBJECT (


idno NUMBER,
name VARCHAR2(30),
phone VARCHAR2(20))
NOT FINAL;
/
2.3.4 Changing a FINAL TYPE to NOT FINAL
You can change inheritance by changing a final type to a not final type
and vice versa with an ALTER TYPE statement.
For example, the following statement changes person_typ to a final type:
Copy
ALTER TYPE person_typ FINAL;
You can only alter a type from NOT FINAL to FINAL if the target type has no
subtypes.

2.3.5 Subtype Creation


You create a subtype using a CREATE TYPE statement that specifies the
immediate parent of the subtype with the UNDER keyword.

Topics:

 Creating a Parent or Supertype Object


 Creating a Subtype Object
 Generalized Invocation
 Creating Multiple Subtypes
 Creating Tables that Contains Supertype and Subtype Objects

2.3.5.1 Creating a Parent or Supertype Object


You can create a parent or supertype object using the CREATE
TYPE statement.
Example 2-14 provides a parent or supertype person_typ object to
demonstrate subtype definitions in Example 2-15, Example 2-18,
and Example 2-19.
Note the show() in Example 2-14. In the subtype examples that follow,
the show() function of the parent type is overridden to specifications for
each subtype using the OVERRIDING keyword.

Example 2-14 Creating the Parent or Supertype person_typ Object

Copy
DROP TYPE person_typ FORCE;
-- if created
CREATE OR REPLACE TYPE person_typ AS OBJECT (
idno NUMBER,
name VARCHAR2(30),
phone VARCHAR2(20),
MAP MEMBER FUNCTION get_idno RETURN NUMBER,
MEMBER FUNCTION show RETURN VARCHAR2)
NOT FINAL;
/

CREATE OR REPLACE TYPE BODY person_typ AS


MAP MEMBER FUNCTION get_idno RETURN NUMBER IS
BEGIN
RETURN idno;
END;
-- function that can be overriden by subtypes
MEMBER FUNCTION show RETURN VARCHAR2 IS
BEGIN
RETURN 'Id: ' || TO_CHAR(idno) || ', Name: ' || name;
END;

END;
/

2.3.5.2 Creating a Subtype Object


A subtype inherits the attributes and methods of the supertype.

These are inherited:


 All the attributes declared in or inherited by the supertype.
 Any methods declared in or inherited by supertype.
Example 2-15 defines the student_typ object as a subtype of person_typ,
which inherits all the attributes declared in or inherited by person_typ and
any methods inherited by or declared in person_typ.

Example 2-15 Creating a student_typ Subtype Using the UNDER Clause

Copy
-- requires Ex. 2-14
CREATE TYPE student_typ UNDER person_typ (
dept_id NUMBER,
major VARCHAR2(30),
OVERRIDING MEMBER FUNCTION show RETURN VARCHAR2)
NOT FINAL;
/

CREATE TYPE BODY student_typ AS


OVERRIDING MEMBER FUNCTION show RETURN VARCHAR2 IS
BEGIN
RETURN (self AS person_typ).show || ' -- Major: ' || major ;
END;

END;
/
The statement that defines student_typ specializes person_typ by adding two
new attributes, dept_id and major and overrides the show method. New
attributes declared in a subtype must have names that are different from
the names of any attributes or methods declared in any of its supertypes,
higher up in its type hierarchy.

2.3.5.3 Generalized Invocation


Generalized invocation provides a mechanism to invoke a method of a
supertype or a parent type, rather than the specific subtype member
method.

Example 2-15 demonstrates this using the following syntax:


Copy
(SELF AS person_typ).show
The student_typ show method first calls the person_typ show method to do the
common actions and then does its own specific action, which is to
append '--Major:' to the value returned by the person_typ show method. This
way, overriding subtype methods can call corresponding overriding parent
type methods to do the common actions before doing their own specific
actions.
Methods are invoked just like normal member methods, except that the
type name after AS should be the type name of the parent type of the type
that the expression evaluates to.

2.3.5.4 Using Generalized Invocation


In Example 2-16, there is an implicit SELF argument just like the implicit
self argument of a normal member method invocation. In this case, it
invokes the person_typ show method rather than the
specific student_typ show method.

Example 2-16 Using Generalized Invocation

Copy
-- Requires Ex. 2-14 and 2-15
DECLARE
myvar student_typ := student_typ(100, 'Sam', '6505556666', 100, 'Math');
name VARCHAR2(100);
BEGIN
name := (myvar AS person_typ).show; --Generalized invocation
END;
/

2.3.5.5 Using Generalized Expression


Generalized expression, like member method invocation, is also supported
when a method is invoked with an explicit self argument.

Example 2-17 Using Generalized Expression

Copy
-- Requires Ex. 2-14 and 2-15
DECLARE
myvar2 student_typ := student_typ(101, 'Sam', '6505556666', 100, 'Math');
name2 VARCHAR2(100);
BEGIN
name2 := person_typ.show((myvar2 AS person_typ)); -- Generalized
expression
END;
/
Double parentheses are used in this example
because ((myvar2 AS person_typ)) is both an expression that must be
resolved and the parameter of the show function.
NOTE: Constructor methods cannot be invoked using this syntax. Also, the
type name that appears after AS in this syntax should be one of the parent
types of the type of the expression for which method is being invoked.

This syntax can only be used to invoke corresponding overriding member


methods of the parent types.

2.3.5.6 Creating Multiple Subtypes


A type can have multiple child subtypes, and these subtypes can also
have subtypes.

Example 2-18 creates another subtype employee_typ under person_typ in


addition to the already existing subtype, student_typ, created in Example 2-
15.

Example 2-18 Creating an employee_typ Subtype Using the UNDER Clause

Copy
-- requires Ex. 2-14

DROP TYPE employee_typ FORCE;


-- if previously created
CREATE OR REPLACE TYPE employee_typ UNDER person_typ (
emp_id NUMBER,
mgr VARCHAR2(30),
OVERRIDING MEMBER FUNCTION show RETURN VARCHAR2);
/

CREATE OR REPLACE TYPE BODY employee_typ AS


OVERRIDING MEMBER FUNCTION show RETURN VARCHAR2 IS
BEGIN
RETURN (SELF AS person_typ).show|| ' -- Employee Id: '
|| TO_CHAR(emp_id) || ', Manager: ' || mgr ;
END;
END;
/

2.3.5.7 Creating a Subtype Under Another Subtype


A subtype can be defined under another subtype.

The new subtype inherits all the attributes and methods that its parent
type has, both declared and inherited. Example 2-19 defines a new
subtype part_time_student_typ under student_typ created in Example 2-15. The
new subtype inherits all the attributes and methods of student_typ and adds
another attribute, number_hours.

Example 2-19 Creating a part_time_student_typ Subtype Using the UNDER


Clause

Copy
CREATE TYPE part_time_student_typ UNDER student_typ (
number_hours NUMBER,
OVERRIDING MEMBER FUNCTION show RETURN VARCHAR2);
/

CREATE TYPE BODY part_time_student_typ AS


OVERRIDING MEMBER FUNCTION show RETURN VARCHAR2 IS
BEGIN
RETURN (SELF AS person_typ).show|| ' -- Major: ' || major ||
', Hours: ' || TO_CHAR(number_hours);
END;

END;
/

2.3.5.8 Creating Tables that Contain Supertype and


Subtype Objects
You can create tables that contain supertype and subtype instances.

You can then populate the tables as shown with


the person_obj_table in Example 2-20.

Example 2-20 Inserting Values into Substitutable Rows of an Object Table

Copy
CREATE TABLE person_obj_table OF person_typ;

INSERT INTO person_obj_table


VALUES (person_typ(12, 'Bob Jones', '650-555-0130'));
INSERT INTO person_obj_table
VALUES (student_typ(51, 'Joe Lane', '1-650-555-0140', 12, 'HISTORY'));

INSERT INTO person_obj_table


VALUES (employee_typ(55, 'Jane Smith', '1-650-555-0144',
100, 'Jennifer Nelson'));

INSERT INTO person_obj_table


VALUES (part_time_student_typ(52, 'Kim Patel', '1-650-555-0135', 14,
'PHYSICS', 20));
You can call the show() function for the supertype and subtypes in the
table with the following:
Copy
SELECT p.show() FROM person_obj_table p;

The output is similar to:

Copy
Id: 12, Name:
Bob Jones
Id: 51, Name:
Joe Lane -- Major: HISTORY
Id: 55, Name:
Jane Smith -- Employee Id: 100, Manager: Jennifer Nelson
Id: 52, Name:
Kim Patel -- Major: PHYSICS, Hours: 20
Note that data that the show() method displayed depends on whether the
object is a supertype or subtype, and if the show() method of the subtype is
overridden. For example, Bob Jones is a person_typ, that is, an supertype.
Only his name and Id are displayed. For Joe Lane, a student_typ,
his name and Id are provided by the show() function of the supertype, and
his major is provided by the overridden show() function of the subtype.
2.3.6 NOT INSTANTIABLE Types and Methods
Types and methods can be declared NOT INSTANTIABLE when they are
created.
NOT INSTANTIABLE types and methods:
 NOT INSTANTIABLE Types
If a type is not instantiable, you cannot instantiate instances of that
type. There are no constructors (default or user-defined) for it. You
might use this with types intended to serve solely as supertypes
from which specialized subtypes are instantiated.
 NOT INSTANTIABLE Methods
A non-instantiable method serves as a placeholder. It is declared but
not implemented in the type. You might define a non-instantiable
method when you expect every subtype to override the method in a
different way. In this case, there is no point in defining the method
in the supertype.
You can alter an instantiable type to a non-instantiable type and vice
versa with an ALTER TYPE statement.
A type that contains a non-instantiable method must itself be declared not
instantiable, as shown in Example 2-21.
2.3.7 Creating a Non-INSTANTIABLE Object Type
If a subtype does not provide an implementation for every inherited non-
instantiable method, the subtype itself, like the supertype, must be
declared not instantiable.

A non-instantiable subtype can be defined under an instantiable


supertype.

Example 2-21 Creating an Object Type that is NOT INSTANTIABLE

Copy
DROP TYPE person_typ FORCE;
-- if previously created
CREATE OR REPLACE TYPE person_typ AS OBJECT (
idno NUMBER,
name VARCHAR2(30),
phone VARCHAR2(20),
NOT INSTANTIABLE MEMBER FUNCTION get_idno RETURN NUMBER)
NOT INSTANTIABLE NOT FINAL;/
2.3.8 Changing an Object Type to INSTANTIABLE
The ALTER TYPE statement can make a non-instantiable type instantiable.
In Example 2-22 an ALTER TYPE statement makes person_typ instantiable.

Example 2-22 Altering an Object Type to INSTANTIABLE

Copy
CREATE OR REPLACE TYPE person_typ AS OBJECT (
idno NUMBER,
name VARCHAR2(30),
phone VARCHAR2(20))
NOT INSTANTIABLE NOT FINAL;/
ALTER TYPE person_typ INSTANTIABLE;
Changing to a Not Instantiable Type

You can alter an instantiable type to a non-instantiable type only if the


type has no columns, views, tables, or instances that reference that type,
either directly, or indirectly, through another type or subtype.

You cannot declare a non-instantiable type to be FINAL. This would actually


be pointless.

You might also like