Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
![]() |
![]() |
Home -> Community -> Usenet -> c.d.o.server -> Re: Object Relational Feature
sburk_at_mooseandpebs.com wrote:
> I have a parent class (PL/SQL) and some child classes that inherit from the
> parent object.
> I have a table of the parent class type
> I have objects of the parent and children classes in the table.
> Each child has an overloaded method of the parent.
> The question is this:
> While inside a procedure in a separate package how do I deref the object
> into the a variable not knowing what type it is so I can execute the method
> of the class it is? If I deref it into the parent class it executes the
> parents method.
The code below illustrates - look specifically at what CheckAnimal()
does. Also notice the constructor hack. PL/SQL does not allow you to
call the parent constructor in your child class's constructor. In
Delphi I would code a child class constructor (and destructor)
something like this:
==
begin
This is not possible in PL/SQL, and thus you need to hack it to ensure that the parent classes constructors up the chain do their thing.
SQL> create or replace type TAnimal as Object
2 (
3 id number(20),
4
5 member procedure AnimalConstructor( self IN OUT TAnimal ), 6 constructor function TAnimal return self as result7 ) not final;
Type created.
SQL> create or replace type body TAnimal as
2
3 member procedure AnimalConstructor( self IN OUT TAnimal ) is
4 begin
5 self.id := TO_NUMBER( TO_CHAR(sysdate,'yyyydddhh24miss')
);
6 end;
7
8 constructor function TAnimal return self as result is
9 begin
10 self.AnimalConstructor; 11 return;
Type body created.
SQL> create or replace type TMammel under TAnimal 2 (
3 cat varchar2(20), 4 subcat varchar2(20), 5 6 member procedure MammelConstructor( self IN OUT TMammel ), 7 constructor function TMammel return self as result8 ) not final;
Type created.
SQL> create or replace type body TMammel as
2
3 member procedure MammelConstructor( self IN OUT TMammel ) is
4 begin
5 self.AnimalConstructor; 6 self.cat := 'WARMBLOODED'; 7 self.subcat := 'UNKNOWN';
13 MammelConstructor; 14 return;
Type body created.
SQL> create or replace type TCow under TMammel
2 (
3 name varchar2(20),
4
5 member procedure CowConstructor( self IN OUT TCow ), 6 constructor function TCow return self as result, 7 member procedure Dump
Type created.
SQL> create or replace type body TCow as
2
3 member procedure CowConstructor( self IN OUT TCow ) is
4 begin
5 self.MammelConstructor; 6 self.subcat := 'BOVINE';
11 self.CowConstructor; 12 return;
18 W( PS1||'id='||self.id ); 19 W( PS1||'cat='||self.cat ); 20 W( PS1||'subcat='||self.subcat ); 21 W( PS1||'name='||self.name );22 end;
Type body created.
SQL> create or replace procedure CheckAnimal( uanimal TAnimal ) is
2 cow$ TCow;
3 begin
4 if uanimal is of (TCow) then 5 cow$ := TREAT(uanimal as TCow); 6 cow$.Dump; 7 end if;
Procedure created.
SQL> declare
2 cow$ TCow;
3 begin
4 cow$ := TCow(); 5 cow$.Name := 'Mina Moo'; 6 CheckAnimal( cow$ );
TCow> id=2005175075520 TCow> cat=WARMBLOODED TCow> subcat=BOVINE TCow> name=Mina Moo
PL/SQL procedure successfully completed.
SQL>
-- BillyReceived on Fri Jun 24 2005 - 01:03:10 CDT
![]() |
![]() |