Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

plsql object type polymorphism

713310Jul 22 2009 — edited Jul 24 2009
Hi everyone.

Is adhoc polymorphism possible in pl/sql using Oracle's object types? Here is some sample code that I tried on an Oracle 10.2.0.3 64-bit database running on Solaris.
Connected.
09:58:58 SQL> create type root_ty as object (
09:59:03   2    a1     varchar2(32 char)
09:59:03   3  ) not final
09:59:03   4  /

Type created.

Elapsed: 00:00:00.17
09:59:05 SQL> create type sub_ty under root_ty (
09:59:10   2    a2     varchar2(32 char)
09:59:10   3  , constructor function sub_ty(str in varchar2) return self as resul
09:59:10   4  , member procedure display_a2
09:59:10   5  ) final
09:59:10   6  /

Type created.

Elapsed: 00:00:00.06
09:59:12 SQL> create or replace type body sub_ty is
09:59:18   2    constructor function sub_ty(str in varchar2)
09:59:18   3      return self as result
09:59:18   4    is
09:59:18   5    begin
09:59:18   6      self.a2 := str;
09:59:18   7      
09:59:18   8      return;
09:59:18   9    end;
09:59:18  10    
09:59:18  11    member procedure display_a2
09:59:18  12    is
09:59:18  13    begin
09:59:18  14      dbms_output.put_line('a2 value is .... '||a2);
09:59:18  15    end;
09:59:18  16  end;
09:59:18  17  /

Type body created.

Elapsed: 00:00:00.04
09:59:20 SQL> set serveroutput on
10:00:31 SQL> declare
10:00:35   2    l_ty    root_ty;
10:00:35   3  begin
10:00:35   4    l_ty := new sub_ty('Woot!');
10:00:35   5    
10:00:35   6    l_ty.display_a2();
10:00:35   7  end;
10:00:35   8  /
  l_ty.display_a2();
       *
ERROR at line 6:
ORA-06550: line 6, column 8:
PLS-00302: component 'DISPLAY_A2' must be declared
ORA-06550: line 6, column 3:
PL/SQL: Statement ignored


Elapsed: 00:00:00.06
10:00:37 SQL> declare
10:00:53   2    l_ty    root_ty;
10:00:53   3  begin
10:00:53   4    l_ty := new sub_ty('Woot!');
10:00:53   5    
10:00:53   6  --  l_ty.display_a2();
10:00:53   7  end;
10:00:53   8  /

PL/SQL procedure successfully completed.

Elapsed: 00:00:00.01
10:00:53 SQL> declare
10:01:30   2    l_ty    sub_ty;
10:01:30   3  begin
10:01:30   4    l_ty := new sub_ty('Woot!');
10:01:30   5    
10:01:30   6    l_ty.display_a2();
10:01:30   7  end;
10:01:30   8  /
a2 value is .... Woot!

PL/SQL procedure successfully completed.
Certainly seems like this should be possible ... Am I missing something simple?

Thanks for any input.

- KR
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 20 2009
Added on Jul 22 2009
19 comments
3,166 views