PL/SQL25 min readBy Priyanshu Pandey

PL/SQL Masterclass: Object-Oriented PL/SQL

Bring Object-Oriented Programming (OOP) to your Oracle Database. Learn how to create custom Object Types, Constructors, Methods, and Inheritance in PL/SQL.

Development Guide · PL/SQL Masterclass

Object-Oriented PL/SQL: Bringing Java Paradigms to the Database

PL/SQL is inherently a procedural language, but it fully supports Object-Oriented Programming (OOP). Learn how to encapsulate logic using Object Types, Constructors, Member Methods, and Inheritance.

25 min read📅August 7, 2026✍️Priyanshu Pandey📚PL/SQL Masterclass

Why OOP in the Database?

Standard PL/SQL is procedural. You write standalone procedures and packages. But what if you are modeling complex business entities, like a Shape that calculates its own area, or a Customer that validates its own email format?

Oracle's Object Types allow you to bind data (attributes) and behavior (methods) together into a single, cohesive unit. You can pass these objects around as parameters, store them in variables, or even store them directly in relational columns!

The Blueprint

Creating an Object Type (The Class)

Creating an Object in Oracle requires two steps, much like creating a Package: the Specification (the structure) and the Body (the implementation).

Let's create a blueprint for an EMPLOYEE_OBJ.

PL/SQL
CREATE OR REPLACE TYPE employee_obj AS OBJECT (
  -- Attributes (Variables)
  emp_id     NUMBER,
  first_name VARCHAR2(50),
  last_name  VARCHAR2(50),
  salary     NUMBER,
  
  -- Member Methods (Functions bound to this instance)
  MEMBER FUNCTION get_full_name RETURN VARCHAR2,
  MEMBER PROCEDURE give_raise(p_amount NUMBER)
)
NOT FINAL; -- NOT FINAL allows this object to be inherited from
/

The Type Body (Methods & Constructors)

Now we implement the behavior in the Type Body.

Notice the SELF keyword. This is the exact equivalent of this in Java or C++. It refers to the specific instance of the object currently executing the method.

PL/SQL
CREATE OR REPLACE TYPE BODY employee_obj AS

  MEMBER FUNCTION get_full_name RETURN VARCHAR2 IS
  BEGIN
    -- SELF refers to this specific instance
    RETURN SELF.first_name || ' ' || SELF.last_name;
  END;
  
  MEMBER PROCEDURE give_raise(p_amount NUMBER) IS
  BEGIN
    SELF.salary := SELF.salary + p_amount;
  END;

END;
/

Instantiating Objects

To use the object, you must instantiate it using its implicit Constructor. Oracle automatically creates a constructor that takes all attributes as arguments.

PL/SQL
DECLARE
  -- Declare a variable of our custom type
  v_emp employee_obj;
BEGIN
  -- Instantiate it using the implicit constructor
  v_emp := employee_obj(101, 'John', 'Doe', 50000);
  
  -- Call a member method
  DBMS_OUTPUT.PUT_LINE('Employee: ' || v_emp.get_full_name());
  
  -- Modify the object's state
  v_emp.give_raise(5000);
  DBMS_OUTPUT.PUT_LINE('New Salary: ' || v_emp.salary);
END;
/
Advanced OOP

Inheritance and Subtyping

Because we declared employee_obj as NOT FINAL, we can create Subtypes that inherit its attributes and methods. Let's create a MANAGER_OBJ.

UNDER specifies inheritance
PL/SQL
CREATE OR REPLACE TYPE manager_obj UNDER employee_obj (
  -- Additional attributes specific to a Manager
  department_name VARCHAR2(50),
  
  -- Overriding a parent method (Polymorphism)
  OVERRIDING MEMBER FUNCTION get_full_name RETURN VARCHAR2
);
/

Now we provide the body for the Manager, overriding the get_full_name method to include a "Manager" prefix.

PL/SQL
CREATE OR REPLACE TYPE BODY manager_obj AS

  OVERRIDING MEMBER FUNCTION get_full_name RETURN VARCHAR2 IS
  BEGIN
    RETURN 'Manager: ' || SELF.first_name || ' ' || SELF.last_name;
  END;

END;
/

Common Gotchas

Important Gotchas

  • !

    Oracle allows you to create tables out of Object Types (CREATE TABLE emps OF employee_obj). Avoid this! It creates tight coupling between your database schema and your PL/SQL code. Changing an object type that is used in a table requires massive, painful data migrations. Stick to using Objects strictly as transient PL/SQL variables.

  • !

    While you can overload methods in standard Packages, doing it in Object Types requires careful management of Custom Constructors to avoid ambiguous signature matches.

Key Takeaways

Key Takeaways

  • Object Types allow you to encapsulate related attributes and behaviors.
  • Use the SELF keyword inside Type Bodies to reference the current instance variables.
  • Use NOT FINAL to allow inheritance, and UNDER to create Subtypes.
  • Use Object Types for PL/SQL modeling, but avoid storing them directly in relational tables.
RetailCoder
All systems operational
v1.0 Live

RC:OMS

Multi-channel order management with double-entry inventory ledger. Amazon, Flipkart, Shopify, WooCommerce — one source of truth.

Launch demo →
v1.0 Live

RC:Storefront

Self-hosted headless e-commerce. Your server, your data, zero transaction fees. Native RC:OMS inventory sync.

Visit Storefront →
Pipeline

RC:Pulse

AI-powered retail analytics and demand forecasting — built natively on top of your RC:OMS and Storefront data.

Request early access →
Built in India 🇮🇳  ·  Architected by Priyanshu PandeyTalk to an engineer →