Inheritance SQL
Inheritance SQL
Topics:
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).
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.
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"
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
Copy
DROP TYPE person_typ FORCE;
-- above necessary if you have previously created object
Topics:
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;
/
END;
/
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;
/
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.
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;
/
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.
Copy
-- requires Ex. 2-14
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.
Copy
CREATE TYPE part_time_student_typ UNDER student_typ (
number_hours NUMBER,
OVERRIDING MEMBER FUNCTION show RETURN VARCHAR2);
/
END;
/
Copy
CREATE TABLE person_obj_table OF person_typ;
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.
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.
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