You can mark an object type as NOT INSTANTIABLE , which prevents objects of that type from being created. You might want to mark an object type as NOT INSTANTIABLE when you want to use that type only as a supertype. For example, you could create a type to represent vehicles and use it as a supertype for another type to represent cars and motorcycles. The following statement creates a type named vehicle_typ , which is marked as NOT INSTANTIABLE :
CREATE TYPE vehicle_typ AS OBJECT (id NUMBER, make VARCHAR2(15), model VARCHAR2(15)) NOT FINAL NOT INSTANTIABLE; /
Note | vehicle_typ is also marked as NOT FINAL . A NOT INSTANTIABLE type cannot be FINAL because you wouldn t be able to use it as a supertype, and that s why vehicle_typ is marked as NOT FINAL . |
The next example creates a type named car_typ that inherits from the vehicle_typ supertype. Notice car_typ has an additional attribute named convertible that records whether the car is a convertible:
CREATE TYPE car_typ UNDER vehicle_typ (convertible CHAR(1)); /
The following example creates a type named motorcycle_typ that inherits from the vehicle_typ supertype. Notice motorcycle_typ has an additional attribute named sidecar that records whether the motorcycle has a sidecar:
CREATE TYPE motorcycle_typ UNDER vehicle_typ (sidecar CHAR(1)); /
The next example creates tables named vehicles , cars and motorcycles , which are object tables that use the types vehicle_typ , car_typ and motorcycle_typ :
CREATE TABLE vehicles OF vehicle_typ; CREATE TABLE cars OF car_typ; CREATE TABLE motorcycles OF motorcycle_typ;
Because vehicle_typ is NOT INSTANTIABLE , you cannot add a row to the vehicles table. If you attempt to do so, the database returns an error; for example:
SQL> INSERT INTO vehicles VALUES ( 2 vehicle_typ(1, 'Toyota', 'MR2', '01-FEB-1955') 3 ); vehicle_typ(1, 'Toyota', 'MR2', '01-FEB-1955') * ERROR at line 2: ORA-22826: cannot construct an instance of a non instantiable type
The following examples add rows to the cars and motorcycles tables:
INSERT INTO cars VALUES (car_typ(1, 'Toyota', 'MR2', 'Y')); INSERT INTO motorcycles VALUES (motorcycle_typ(1, 'Harley-Davidson', 'V-Rod', 'N'));
The final example queries the cars and motorcycles tables:
SELECT * FROM cars; ID MAKE MODEL C ---------- --------------- --------------- - 1 Toyota MR2 Y SELECT * FROM motorcycles; ID MAKE MODEL S ---------- --------------- --------------- - 1 Harley-Davidson V-Rod N