This discussion is archived
4 Replies Latest reply: May 30, 2012 6:57 PM by user12075620 RSS

dynamic method dispatch pl/sql

user12075620 Newbie
Currently Being Moderated
Hi Experts,

Could you please give a simple example for dynamic method dispatch in PL/SQL and when to use ? Your help on this would be much appreciated...

Regards,
Boris.
  • 1. Re: dynamic method dispatch pl/sql
    BillyVerreynne Oracle ACE
    Currently Being Moderated
    From the manual Oracle® Database Object-Relational Developer's Guide:
    When you call a method using an instance of a subtype, the actual method 
    that is executed depends on declarations in the type hierarchy. If the subtype 
    overrides the method that it inherits from its supertype, the call uses the 
    subtype implementation. Otherwise, the call uses the supertype implementation. 
    This capability is known as dynamic method dispatch.
    This simply means that a parent class has a method call FooSomething() and a child class does not override it, thus when you instantiate a child object and call FooSomething(), the parent class method is executed as the child class does not provide an implementation for the method.

    It is a basic o-o concept. Anything specific that does not make sense for you in the PL/SQL o-o context?
  • 2. Re: dynamic method dispatch pl/sql
    user12075620 Newbie
    Currently Being Moderated
    Thanks Billy for your response. Much appreciated. To be honest, I could not find any practical use to use this method from my knowledge. I would appreciate if you explain with some simple example and when to use this method.

    Regards,
    Boris
  • 3. Re: dynamic method dispatch pl/sql
    BillyVerreynne Oracle ACE
    Currently Being Moderated
    user12075620 wrote:
    Thanks Billy for your response. Much appreciated. To be honest, I could not find any practical use to use this method from my knowledge. I would appreciate if you explain with some simple example and when to use this method.
    Well, here is a simplistic example:
    SQL> create or replace type TPerson is object(
      2          idNumber        varchar2(15),
      3          surname         varchar2(100),
      4          initials        varchar2(10),
      5          dateOfBirth     date,
      6
      7          member function Age return number,
      8          member function Label return varchar2
      9  ) not final;
     10  /
    
    Type created.
    
    SQL>
    SQL> create or replace type body TPerson as
      2          member function Age return number is
      3          begin
      4                  return(
      5                          trunc(
      6                                  months_between(Sysdate, self.dateOfBirth) / 12
      7                          )
      8                  );
      9          end;
     10
     11          member function Label return varchar2 is
     12          begin
     13                  return(
     14                          upper(self.initials)||' '||upper(self.surname)||
     15                          ' ('||self.idNumber||')'
     16                  );
     17          end;
     18  end;
     19  /
    
    Type body created.
    
    SQL>
    SQL> create or replace type TStudent under TPerson(
      2          studentID       number,
      3          enrollmentDate  date,
      4
      5          member function EnrollmentPeriod return number,
      6          overriding member function Label return varchar2
      7  );
      8  /
    
    Type created.
    
    SQL>
    SQL> create or replace type body TStudent as
      2
      3          member function EnrollmentPeriod return number is
      4          begin
      5                  return(
      6                          round(
      7                                  months_between(Sysdate, self.enrollmentDate) / 12,
      8                                  2
      9                          )
     10                  );
     11          end;
     12
     13          overriding member function Label return varchar2 is
     14          begin
     15                  return(
     16                          'Student: '||to_char(self.studentID)||
     17                          ' ('||initcap(Surname)||')'
     18                  );
     19          end;
     20  end;
     21  /
    
    Type body created.
    
    SQL>
    SQL> declare
      2          p       TStudent;
      3  begin
      4          p := new TStudent( '1234567890', 'smith', 'jj', to_date('1990','yyyy'), 789321, to_date('2012','yyyy') );
      5
      6          --// calling a parent class function via the child class - the
      7          --// client does not provide an overriding member function
      8          DBMS_OUTPUT.put_line( 'Age= '||p.Age() );
      9
     10          --// calling a child method that the child overrides from
     11          --// the parent class (method exists in both)
     12          --// (dynamic dispatch)
     13          DBMS_OUTPUT.put_line( 'Label= '||p.Label() );
     14
     15          --// calling a method introduced by the child class only
     16          DBMS_OUTPUT.put_line( 'EnrollmentPeriod= '||p.EnrollmentPeriod() );
     17
     18          --// calling the parent method instead of the overriding
     19          --// child method ("forcing" the "dynamic dispatch" to use
     20          --// parent class method)
     21          DBMS_OUTPUT.put_line( 'parent Label= '||(p as TPerson).Label() );
     22  end;
     23  /
    Age= 22
    Label= Student: 789321 (Smith)
    EnrollmentPeriod= .07
    parent Label= JJ SMITH (1234567890)
    
    PL/SQL procedure successfully completed.
    
    SQL>
  • 4. Re: dynamic method dispatch pl/sql
    user12075620 Newbie
    Currently Being Moderated
    Thanks billy for your response. I was on vacation and I could not respond to you immediately. Sorrt for the delayed response.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points