This discussion is archived
4 Replies Latest reply: May 21, 2012 7:14 PM by user12075620 RSS

Overriding vs hiding in plsql

user12075620 Newbie
Currently Being Moderated
Hi Experts,

I am going through the oracle documentation to understand the concept of overriding and hiding. This is the definition from the oracle documentation

"Redefining an inherited method to customize its behavior in a subtype is called overriding, in the case of member methods, or hiding, in the case of static methods."

Link: http://docs.oracle.com/cd/B28359_01/appdev.111/b28371/adobjbas.htm


Could you please provide an example of hiding and how it differs from overridding?

Thanks in advance.

Regards,
Boris
  • 1. Re: Overriding vs hiding in plsql
    BillyVerreynne Oracle ACE
    Currently Being Moderated
    user12075620 wrote:

    "Redefining an inherited method to customize its behavior in a subtype is called overriding, in the case of member methods, or hiding, in the case of static methods."
    Consider this base class:
    create or replace type TParentClass as object(
      name varchar2(10),
      static function ClassName return varchar2,
      member function DoSomething return varchar2
    ) not final;
    It defines a static class function and a member class function. Now both can be overridden in a subclass. E.g.
    create or replace type TChildClass under TParentClass (
      static function ClassName return varchar2,   // hiding class static method
      overriding member function DoSomething return varchar2  // overriding class member method
    ) final;
    Edited by: Billy Verreynne on May 21, 2012 6:50 AM.
    Correction made - the clause overriding is not needed for static class methods, when the subclass defines a static method with the same name as the parent class static method (thus hiding the parent static method via the subclass)
  • 2. Re: Overriding vs hiding in plsql
    user12075620 Newbie
    Currently Being Moderated
    Thanks Billy for the explanation. If both the class can be overriden, how the hiding differs from overriding. It would be great if you give me a complete example.
    When the overriding or hiding would be useful?

    I appreciate your service to oracle community

    Regards,
    Boris
  • 3. Re: Overriding vs hiding in plsql
    BillyVerreynne Oracle ACE
    Currently Being Moderated
    user12075620 wrote:
    If both the class can be overriden, how the hiding differs from overriding.
    The static class method is not overridden (I fixed the example I posted above - was not close to an Oracle database over the weekend to confirm the syntax).

    The static class method is redefined in the subclass - which means that one cannot call the parent static class method via the child class.
    It would be great if you give me a complete example.
    A basic example below.
    SQL> create or replace type TParentClass as object(
      2          name varchar2(10),
      3          static function ClassName return varchar2,
      4          member function DoSomething return varchar2
      5  ) not final;
      6  /
    
    Type created.
    
    SQL> 
    SQL> create or replace type body TParentClass as
      2          static function ClassName return varchar2 is
      3          begin
      4                  return( 'TParentClass' );
      5          end;
      6  
      7          member function DoSomething return varchar2 is
      8          begin
      9                  return( 'Hello world from '||TParentClass.ClassName );
     10          end;
     11  end;
     12  /
    
    Type body created.
    
    SQL> 
    SQL> create or replace type TChildClass under TParentClass (
      2          static function ClassName return varchar2,
      3          overriding member function DoSomething return varchar2
      4  ) final;
      5  /
    
    Type created.
    
    SQL> 
    SQL> create or replace type body TChildClass as
      2          static function ClassName return varchar2 is
      3          begin
      4                  return( 'TChildClass' );
      5          end;
      6  
      7          overriding member function DoSomething return varchar2 is
      8          begin
      9                  return( 'Hello world from '||TChildClass.ClassName );
     10          end;
     11  end;
     12  /
    
    Type body created.
    
    SQL> 
    SQL> declare
      2          obj     TParentClass;
      3  begin
      4          --// create a parent class object
      5          obj := new TParentClass(null);
      6          DBMS_OUTPUT.put_line( 'obj method DoSomething()  = '||obj.DoSomething() );
      7  end;
      8  /
    obj method DoSomething()  = Hello world from TParentClass
    
    PL/SQL procedure successfully completed.
    
    SQL> 
    SQL> declare
      2          obj     TParentClass;
      3  begin
      4          --// create a child object
      5          obj := new TChildClass(null);
      6          DBMS_OUTPUT.put_line( 'obj method DoSomething() = '||obj.DoSomething() );
      7          --// use "general invocation" to call the overridden parent method
      8          DBMS_OUTPUT.put_line( 'parent method DoSomething() = '||(obj as TParentClass).DoSomething() );
      9  end;
     10  /
    obj method DoSomething() = Hello world from TChildClass
    parent method DoSomething() = Hello world from TParentClass
    
    PL/SQL procedure successfully completed.
    
    SQL> 
    SQL> begin
      2          --// not possible to call a hidden parent static class method via
      3          --// the child class
      4          DBMS_OUTPUT.put_line( 'class = '||(TChildClass as TParentClass).ClassName() );
      5  end;
      6  /
            DBMS_OUTPUT.put_line( 'class = '||(TChildClass as TParentClass).ClassName() );
                                               *
    ERROR at line 4:
    ORA-06550: line 4, column 37:
    PLS-00306: wrong number or types of arguments in call to 'TCHILDCLASS'
    ORA-06550: line 4, column 2:
    PL/SQL: Statement ignored
    When the overriding or hiding would be useful?
    Hiding the static method as shown above allows the child class to hide the parent's static class method and implement its own static method. The simplistic example above would not work correctly if the child static method showed the name of the parent class. So the child class has to hide the parent static class method and provide its own class method instead.

    Normal class methods can be overridden - normal o-o behaviour. The child class provides a different or extended features using the same member method as the parent class. However, the parent method is not hidden. It can still be accessed - and in PL/SQL, it is accessed via a feature called "+generalised invocation/expressions+" as detailed in Oracle® Database Object-Relational Developer's Guide.
  • 4. Re: Overriding vs hiding in plsql
    user12075620 Newbie
    Currently Being Moderated
    Billy you are simply the best!!! I am much impressed with your lucid explanation.

Legend

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