Exforsys

H I D E

Home arrow Technical Training arrow Oracle 11g Tutorial

Oracle 11g Subprogram Overloading Page - 2

Page 2 of 2
Author: Saurabh Gupta     Published on: 17th Jun 2011

Oracle 11g Subprogram Overloading

Overloading Usage guidelines/restrictions

If the parameter list of the overloaded program units appears same in terms of count and their data types, Oracle raises exception and restricts the creation of the package. Note that Oracle allows overloading, not duplication.

Special attention should be kept towards data type of the parameters. Even if the parameter data types are different, but belong to the same data type family, Oracle prevents the overloading of subprograms.

Code [5]:

Two procedures L_CALC accept NUMBER and INTEGER type inputs. But the package body fails to compile the program unit because INTEGER and NUMBER belong to the same data type family.

Sample Code
  1. DECLARE
  2.    PROCEDURE L_CALC(A NUMBER)
  3.    IS
  4.    BEGIN
  5.       DBMS_OUTPUT.PUT_LINE(A*10);
  6.    END;
  7.  
  8.    PROCEDURE L_CALC(A INTEGER)
  9.    IS
  10.    BEGIN
  11.       DBMS_OUTPUT.PUT_LINE(A*10);
  12.    END;
  13. BEGIN
  14.    L_CALC(7);
  15. END;
  16. /
  17.    L_CALC(7);
  18.    *
  19. ERROR at line 13:
  20. ORA-06550: line 13, COLUMN 4:
  21. PLS-00307: too many declarations of 'L_CALC' match this call
  22. ORA-06550: line 13, COLUMN 4:
  23. PL/SQL: Statement ignored
Copyright exforsys.com


Oracle allows the creation of a overloaded function with same signature but different RETURN type. But at the time of execution, optimizer finds conflict of headers and raises exception PLS-00307.


Overloading is not possible if the parameters differ only in their passage mode.

Code [6]:

Locally overloaded procedures L_ADD fail to overload due to same signatures.

Sample Code
  1. SQL> DECLARE
  2.   2     PROCEDURE L_ADD(A IN NUMBER, B IN NUMBER) ----//Local procedure 1
  3.   3     IS
  4.   4     BEGIN
  5.   5        DBMS_OUTPUT.PUT_LINE(A+B);
  6.   6     END;
  7.   7     PROCEDURE L_ADD(A IN NUMBER, B OUT NUMBER)  ----//Local procedure 2
  8.   8     IS
  9.   9     BEGIN
  10.  10        B:= A;
  11.  11     END;
  12.  12  BEGIN
  13.  13     L_ADD(1,2);
  14.  14  END;
  15.  15  /
  16.    L_ADD(1,2);
  17.    *
  18. ERROR at line 13:
  19. ORA-06550: line 13, COLUMN 4:
  20. PLS-00307: too many declarations of 'L_ADD' match this call
  21. ORA-06550: line 13, COLUMN 4:
  22. PL/SQL: Statement ignored
Copyright exforsys.com


Metadata for overloaded subprograms in Packages

Package metadata is captured in USER_PROCEDURES dictionary view. For overloaded subprograms, the system generates a sequence id, in order of their prototyping within the package.

Run the below query to view the package metadata along with the overloaded subprogram sequence.

Code [7]

Sample Code
  1. SQL> SELECT OBJECT_NAME, OBJECT_TYPE, PROCEDURE_NAME, SUBPROGRAM_ID, OVERLOAD
  2.      FROM USER_PROCEDURES
  3.      WHERE OBJECT_NAME='PKG_OVERLOAD_TEST';
Copyright exforsys.com


Overloading in Object types

In Object types, polymorphism is practiced through Overloaded and Overridden methods. Overloading is practiced in inherited object types where the subtypes have a member method with the same name as that of inherited one, but differ in nature of parameters. Based on the member methods signatures, optimizer routes the invocation calls and produces the output.

For example, a super (parent) type CALC_OT contains multiple calculation member methods, one of which is to add 2 numbers as shown in the below definition.

Sample Code
  1. CREATE OR REPLACE TYPE CALC_OT AS OBJECT
  2. ( …
  3.  MEMBER PROCEDURE ADD(P1 NUMBER, P2 NUMBER)
  4. );
Copyright exforsys.com


Now a sub (child) type ADD_OT adds inherited number inputs with its owned ones. Its member subprogram can be overloaded and created with the same name as the inherited one.

Sample Code
  1. CREATE OR REPLACE TYPE ADD_OT UNDER CALC_OT
  2. (…
  3. …
  4. MEMBER PROCEDURE ADD(P3 NUMBER)
  5. );
Copyright exforsys.com


Overriding is another technique to create a subprogram in object type hierarchy with the same name. But unlike overloading, here the member method is redefined in the sub (child) type body. This eclipses the subprogram definition available in the type body of super (parent) type. If the subtype extends to some other types too, they inherit the redefined version of the overridden subprogram and not the original version.

When a type variable invokes the member method, Oracle executes the associated member subprogram with that type only.

Code [8]:

A super type OT_OVERLOAD_TEST has been created, which contains DISPLAY method to print the type attribute values. The type has been extended to OT_SUB_OVERLOAD, which inherits its parent’s attributes and methods. But it declares an overriding method DISPLAY to print the alternate name for it id.

Sample Code
  1. SQL> CREATE OR REPLACE TYPE OT_OVERLOAD_TEST AS OBJECT
  2.         (ID NUMBER,
  3.          NAME VARCHAR2(100),
  4.          MEMBER FUNCTION DISPLAY RETURN VARCHAR2
  5.          )
  6.       NOT FINAL;
  7.         /      
  8.  
  9. Type created.
  10.  
  11. SQL> CREATE OR REPLACE TYPE BODY OT_OVERLOAD_TEST AS
  12.         MEMBER FUNCTION DISPLAY RETURN VARCHAR2
  13.         IS
  14.         BEGIN
  15.            RETURN 'Name for the id:'||to_char(ID)||' is '||NAME;
  16.         END;
  17.     END;
  18.     /
  19. Type body created.
  20.  
  21. SQL> CREATE OR REPLACE TYPE OT_SUB_OVERLOAD UNDER OT_OVERLOAD_TEST
  22.      (ALTER_NAME VARCHAR2(100),
  23.       OVERRIDING MEMBER FUNCTION DISPLAY RETURN VARCHAR2);
  24.       /
  25.  
  26. Type created.
  27.  
  28. SQL> CREATE OR REPLACE TYPE BODY OT_SUB_OVERLOAD AS
  29.       OVERRIDING MEMBER FUNCTION DISPLAY RETURN VARCHAR2
  30.       IS
  31.       BEGIN
  32.          RETURN 'Alternate name for the id:'||to_char(self.ID)||' is '||ALTER_NAME;
  33.       END;
  34.     END;
  35.     /
  36.  
  37. Type body created.
  38.  
  39. SQL> DECLARE
  40.   2  l_parent ot_overload_test := ot_overload_test(100,'Exforsys');
  41.   3  l_child ot_sub_overload := ot_sub_overload(100,'Exforsys','Oracle 11g tutorials');
  42.   4  BEGIN
  43.   5  dbms_output.put_line(l_parent.display());
  44.   6  Dbms_Output.Put_Line(L_Child.Display());
  45.   7  END;
  46.   8  /
  47. Name FOR the id:100 IS Exforsys
  48. Alternate name FOR the id:100 IS Oracle 11g tutorials
  49.  
  50. PL/SQL procedure successfully completed.
Copyright exforsys.com


Read Next: Conditional compilation in Oracle PL/SQL


 

Oracle 11g Tutorial

 

Comments