Exforsys

Home arrow Technical Training arrow Oracle 11g Tutorial

Oracle 11g Subprogram Overloading

Page 1 of 2
Author:      Published on: 17th Jun 2011

Polymorphism is an Object Oriented Programming language feature which allows the objects to accept same input and behave differently. It can be implemented in three ways i.e. overriding, hiding, and overloading.

In Oracle, overloading is one of the polymorphism techniques which allow the creation of more than one program units, within the vicinity of block, with the same name but different parameter lists.

In this tutorial, we shall move ahead to understand the overloading techniques and restrictions in Oracle.

Overloading: Background

Overloading is not new to Oracle. It is one of the most extensively used concepts in the making of Oracle built in functions. Many of the built in functions, which are defined in the STANDARD package, DBMS_OUTPUT or others packages, are overloaded.

Code [1]:

PUT_LINE function can be used with numeric, string or date inputs as shown below.

Sample Code
  1. SQL> exec DBMS_OUTPUT.PUT_LINE(100);
  2. 100
  3.  
  4. PL/SQL procedure successfully completed.
  5.  
  6. SQL> EXEC DBMS_OUTPUT.PUT_LINE('Oracle');
  7. Oracle
  8.  
  9. PL/SQL procedure successfully completed.
  10.  
  11. SQL> EXEC DBMS_OUTPUT.PUT_LINE(SYSDATE);
  12. 18-MAY-11
  13.  
  14. PL/SQL procedure successfully completed.
Copyright exforsys.com


Another overloaded function TO_CHAR can be used with date, number and string inputs. Its overloaded headers are as below.

Sample Code
  1. FUNCTION TO_CHAR (RIGHT DATE) RETURN VARCHAR2;
  2. FUNCTION TO_CHAR (LEFT NUMBER) RETURN VARCHAR2;
  3. FUNCTION TO_CHAR (LEFT DATE, RIGHT VARCHAR2) RETURN VARCHAR2;
  4. FUNCTION TO_CHAR (LEFT NUMBER, RIGHT VARCHAR2) RETURN VARCHAR2;
Copyright exforsys.com


Many mathematical functions accept all type of input which belongs to the same data type family. This implies that Oracle kernel is quite familiar with overloading and its benefits.

Overloading of Local and Packaged subprograms

Oracle allows overloading of subprograms in local scope, packaged scope or object type scope. The subprograms declared in these scopes can exist with same name with different signatures. Note that a ‘subprogram signature’ is a component of its name and parameter list only. As the subprogram name is same, the parameters passed to the subprograms should different in count, order, or belonging data type family.

Only local or packaged procedure and functions can be overloaded. Standalone (or stored) subprograms cannot be overloaded as they need to obey the rule of unique name and namespace categorization. Object type methods contained by super type and subtype can be overloaded and overridden too.

I shall demonstrate overloading with the help of a sample program in Code [2]

Code [2]

Sample Code
  1. CREATE OR REPLACE PACKAGE PKG_OVERLOAD_TEST
  2. IS
  3.    PROCEDURE P_TEST1(A NUMBER, B NUMBER);
  4.    PROCEDURE P_TEST1(A VARCHAR2);
  5. END;
  6.  
  7. CREATE OR REPLACE PACKAGE BODY PKG_OVERLOAD_TEST
  8. IS
  9.    PROCEDURE P_TEST1(A NUMBER, B NUMBER)
  10.    IS
  11.    BEGIN
  12.       DBMS_OUTPUT.PUT_LINE('Sum of two numbers is:'||to_char(A+B));
  13.    END;
  14.  
  15.    PROCEDURE P_test1( a varchar2)
  16.    IS
  17.    BEGIN
  18.       DBMS_OUTPUT.PUT_LINE('The input string is:'||A);
  19.    END;
  20. END;
Copyright exforsys.com


The overloaded subprograms show different behavior based on the inputs provided.

Code [3]:

Testing the procedure call with two number inputs.

Sample Code
  1. SQL> BEGIN
  2.   2     PKG_OVERLOAD_TEST.P_TEST1(1,2);
  3.   3  END;
  4.   4  /
  5. Sum of two numbers IS:3
  6.  
  7. PL/SQL procedure successfully completed.
Copyright exforsys.com


When the PL/SQL engine receives the subprogram invocation call, it compares the inputs with the parameter list available for overloaded units. It executes only the one whose signature matches with the input and produces the output.

Ads

In the Code [3], the two inputs were of number type. Optimizer matches them with the overloaded modules available for PKG_OVERLOAD_TEST. Parameter list for the first subprogram header matches the call and is executed.

Similarly, second overloaded subprogram can also be tested as below with a string input.

Code [4]

Sample Code
  1. SQL> BEGIN
  2.   2     PKG_OVERLOAD_TEST.P_TEST1('Exforsys');
  3.   3  END;
  4.   4  /
  5. The input string IS:Exforsys
  6.  
  7. PL/SQL procedure successfully completed.
Copyright exforsys.com




 
This tutorial is part of a Oracle 11g Tutorial tutorial series. Read it from the beginning and learn yourself.

Oracle 11g Tutorial

 

Comments