Sub topic 5
Public versus Private: What the Specification Hides
Prerequisites
This chapter assumes that you know basic SQL, PL/SQL variables and control flow, standalone stored procedures and functions, and basic exception handling.
A PL/SQL package has two parts:
- The package specification declares what callers can use.
- The package body implements the declared subprograms and can contain private code.
Think of the specification as a published interface. Callers need to know the names, parameters, return types, and public data. They do not need to know how those operations work internally.
flowchart TD
Caller[Calling PL/SQL code] --> Spec[Package specification]
Spec --> Public[Public procedures, functions, constants, types, exceptions]
Body[Package body] --> Public
Body --> Private[Private helpers and variables]
Caller -. cannot access .-> Privatecaption: Package visibility alt: A caller reaches public package elements through the specification, while private helpers and variables remain accessible only inside the package body.
Declaring public elements
Any declaration in the specification is public. A caller can reference it through the package name and dot notation.
This example publishes a constant, an exception, a function, and a procedure:
CREATE OR REPLACE PACKAGE emp_api AS
c_max_raise_pct CONSTANT NUMBER := 20;
e_raise_too_large EXCEPTION;
PRAGMA EXCEPTION_INIT(e_raise_too_large, -20001);
FUNCTION get_salary(p_employee_id IN employees.employee_id%TYPE)
RETURN employees.salary%TYPE;
PROCEDURE raise_salary(
p_employee_id IN employees.employee_id%TYPE,
p_pct IN NUMBER);
END emp_api;
/The specification does not contain the function or procedure logic. It only describes the public contract. The %TYPE attribute gives a parameter the same data type as an existing table column. This helps keep the interface aligned with the table definition.
The package body must implement every public procedure and function declared in the specification. It can also declare additional private elements.
Implementing private elements
A declaration in the body, but not in the specification, is private. Only code inside that package body can call or reference it.
CREATE OR REPLACE PACKAGE BODY emp_api AS
FUNCTION is_valid_pct(p_pct IN NUMBER) RETURN BOOLEAN IS
BEGIN
RETURN p_pct > 0 AND p_pct <= c_max_raise_pct;
END is_valid_pct;
FUNCTION get_salary(p_employee_id IN employees.employee_id%TYPE)
RETURN employees.salary%TYPE IS
l_salary employees.salary%TYPE;
BEGIN
SELECT salary
INTO l_salary
FROM employees
WHERE employee_id = p_employee_id;
RETURN l_salary;
END get_salary;
PROCEDURE raise_salary(
p_employee_id IN employees.employee_id%TYPE,
p_pct IN NUMBER) IS
BEGIN
IF NOT is_valid_pct(p_pct) THEN
RAISE e_raise_too_large;
END IF;
UPDATE employees
SET salary = salary * (1 + p_pct / 100)
WHERE employee_id = p_employee_id;
END raise_salary;
END emp_api;
/is_valid_pct is a private helper. It centralizes validation without adding another operation that callers must understand or maintain.
A caller can use the public elements:
BEGIN
emp_api.raise_salary(100, 10);
DBMS_OUTPUT.PUT_LINE(
'New salary: ' || emp_api.get_salary(100)
);
EXCEPTION
WHEN emp_api.e_raise_too_large THEN
DBMS_OUTPUT.PUT_LINE(
'Raise exceeds ' || emp_api.c_max_raise_pct || '%'
);
END;
/This caller cannot write emp_api.is_valid_pct(10). The name is absent from the specification, so it is not visible outside the package. Attempting to call it produces a compilation error.
Choosing the visibility boundary
Use the specification for elements that form a stable service for other code:
- Procedures and functions that callers need
- Constants that callers must read
- Types required in public parameters or results
- Exceptions that callers should handle
Keep implementation details private:
- Validation helpers
- SQL-specific helper routines
- Temporary variables
- Internal constants
- Cursors used only by the body
A public variable is also visible and writable by callers. That makes it part of the package’s external state and can allow callers to violate rules. Prefer public constants, functions, and procedures when possible.
Summary
The specification controls visibility. The body supplies implementation and may add private declarations. This separation lets a package hide complexity, enforce rules in one place, and expose a small interface that is easier to use and maintain.
Exercise
Is g_default_currency public or private if it is declared in the package body but not in the specification?
Show answer
It is private. Only code inside the package body can reference it.