CREATE OR REPLACE TYPE food_ot AS OBJECT (
name VARCHAR2 (100),
food_group VARCHAR2 (50),
grown_in VARCHAR2 (100),
NOT INSTANTIABLE MEMBER FUNCTION price
RETURN NUMBER
)
NOT FINAL NOT INSTANTIABLE;
Type created.
CREATE OR REPLACE TYPE dessert_ot UNDER food_ot (
contains_chocolate CHAR (1)
, year_created NUMBER (4)
, OVERRIDING MEMBER FUNCTION price
RETURN NUMBER
)
NOT FINAL;
Type created.
CREATE OR REPLACE TYPE BODY dessert_ot
IS
OVERRIDING MEMBER FUNCTION price
RETURN NUMBER
IS
multiplier NUMBER := 1;
BEGIN
DBMS_OUTPUT.put_line ('Dessert price!');
IF SELF.contains_chocolate = 'Y'
THEN
multiplier := 2;
END IF;
IF SELF.year_created < 1900
THEN
multiplier := multiplier + 0.5;
END IF;
RETURN (10.00 * multiplier);
END;
END;
Type created.
DECLARE
l_food food_ot ;
BEGIN
l_food := dessert_ot ('Apple', 'Fruit', 'Tree', 'N', -5000);
DBMS_OUTPUT.PUT_LINE (l_food.name);
END;
Statement processed.
Apple