Did you know PL/SQL generalized invocation has been available since 11g? The generalized invocation allows a subtype to invoke a method of a parent type (a supertype) using the following syntax:
(SELF AS supertype_name).method_name
Yes, this is the concept of inheritance applied to PL/SQL. Check the example below to understand it. First, to create the original type:
CREATE OR REPLACE TYPE type_test AS OBJECT (long_text VARCHAR2(50), MEMBER FUNCTION return_text RETURN VARCHAR2) NOT FINAL; / CREATE OR REPLACE TYPE BODY type_test AS MEMBER FUNCTION return_text RETURN VARCHAR2 IS BEGIN RETURN long_text; END; END; /
Now to create a subtype of this object, which adds a new attribute and method as well as overriding the member’s function.
CREATE OR REPLACE TYPE subtype_test UNDER type_test (short_text VARCHAR2(20), OVERRIDING MEMBER FUNCTION return_text RETURN VARCHAR2,MEMBER FUNCTION return_parent_text RETURN VARCHAR2); / CREATE OR REPLACE TYPE BODY subtype_test AS OVERRIDING MEMBER FUNCTION return_text RETURN VARCHAR2 IS BEGIN RETURN (self AS type_test).return_text || short_text; END; MEMBER FUNCTION return_parent_text RETURN VARCHAR2 IS BEGIN RETURN (self AS type_test).return_text; END; END; /
And when calling:
SET SERVEROUTPUT ON DECLARE l_subtype subtype_test := subtype_test('This is the original text.', ' Subtype Addition!'); BEGIN DBMS_OUTPUT.put_line('Parent Content= ' || l_subtype.return_parent_text); DBMS_OUTPUT.put_line('Subtype Content= ' || l_subtype.return_text); END; / Parent Content= This is the original text. Subtype Content= This is the original text. Subtype Addition!
A type can invoke the member functions of any parent type in this way, regardless of the depth of the inheritance.
Interesting, right?
Hope you like it!
If you have any questions or thoughts about this, please feel free to leave them in the comments.
Share this
Previous story
← Datascape Podcast Episode 47: March 2021 Cloud Update Show
You May Also Like
These Related Stories
How to Create an Oracle SE2 Database and Avoid Licensing Problems
How to Create an Oracle SE2 Database and Avoid Licensing Problems
Jun 16, 2022
9
min read
Oracle Database 12c: Network Recovery in RMAN
Oracle Database 12c: Network Recovery in RMAN
Aug 14, 2013
12
min read
How to mine an RMAN Log
How to mine an RMAN Log
Jun 24, 2019
3
min read
No Comments Yet
Let us know what you think