This document explains about Object abstraction in PL/SQL. An Object abstraction refers to the high-level description or model of a real-world entity. Abstractions keep our daily lives manageable by suppressing irrelevant detail. For example, to drive a car, you need not know how its engine works. A simple interface consisting of a gearshift, steering wheel, accelerator, and brake, lets you use the car effectively. The details of what happens under the hood are not important for day-to-day driving.
The following technology has been used to achieve abstraction of object types,
- PL/SQL
Abstraction is the concept of object-oriented programming that “shows” only essential attributes and “hides” unnecessary information. The main purpose of abstraction is hiding the unnecessary details from the users. Abstraction is selecting data from a larger pool to show only relevant details of the object to the user. It helps in reducing programming complexity and efforts. So, it can be applied to achieve reusability of components.
4. Steps with Screenshot
Steps to be followed,
Step 1: Create abstract object type (Super type) by using the below syntax.
Syntax:
CREATE TYPE <PARENT OBJECT TYPE NAME> AS OBJECT
(<attribute_1> <datatype>;
<attribute_2><datatype>;
NOT INSTANTIABLE MEMBER FUNCTION/PROCEDURE
) NOT FINAL NOT INSTANTIABLE;
Code:
CREATE OR REPLACE TYPE CAR AS OBJECT (
BRAND_NAME VARCHAR2 (100),
TRANSMISSION_TYPE VARCHAR2 (50),
END_RANGE VARCHAR2 (100),
NOT INSTANTIABLE MEMBER FUNCTION TOTAL_PRICE
RETURN NUMBER
)
NOT FINAL NOT INSTANTIABLE;
Note: If “Not final” keyword is not mentioned as part of the object type, oracle engine will take as default value. Default value for object type is “Final”.
Step 2: Create Sub type object by using below syntax.
Syntax:
CREATE TYPE <CHILD OBJECT TYPE NAME> UNDER PARENT OBJECT TYPE NAME
(<attribute_1> <datatype>;
<attribute_2><datatype>;
) NOT FINAL;
Code:
CREATE OR REPLACE TYPE CATEGORY UNDER CAR (
ENGINE_TYPE CHAR (1) ,
PRICE NUMBER,
OVERRIDING MEMBER FUNCTION TOTAL_PRICE
RETURN NUMBER
)
NOT FINAL;
Note: “NOT FINAL” means subtypes can be derived and “FINAL”, (default) means that no subtypes can be derived from it.
Step 3: Create a body for the Sub type, which should defines the purpose of member functions.
Syntax:
CREATE TYPE BODY<object_type_name> AS OBJECTIS MEMBER
[PROCEDURE|FUNCTION]
<member_name>
IS
<declarative section>
BEGIN
<execution part>
END;
Code:
CREATE OR REPLACE TYPE BODY CATEGORY
IS
OVERRIDING MEMBER FUNCTION TOTAL_PRICE
RETURN NUMBER
IS
TOTAL_COST NUMBER;
BEGIN
IF SELF.ENGINE_TYPE = ‘P’ THEN TOTAL_COST := SELF.PRICE+(SELF.PRICE*0.2);
END IF;
IF SELF.ENGINE_TYPE = ‘D’ THEN TOTAL_COST := SELF.PRICE+(SELF.PRICE*0.1);
END IF;
RETURN (TOTAL_COST);
END;
Note:”Self” keyword is used to get the owned values of attributes from the table.
Note: Overriding and hiding redefine an inherited method to make it do something different in the subtype. For example, in the above query super object type’s member function which is in not instantiable state. So it has been overridden by sub object type member function attribute.
Step 4: To verify object abstraction on Super object type,we can try to initialize abstracted object type and check the output.
Code:
DECLARE
ABSTRACT_VEHICLE_DETAILS CAR:= CAR (‘HONDA’, ‘AMT’, ‘HIGH’, ‘P’, 1000000);
BEGIN
DBMS_OUTPUT.PUT_LINE (‘BRAND’||’:’||’ ‘||ABSTRACT_VEHICLE_DETAILS.BRAND_NAME);
DBMS_OUTPUT.PUT_LINE(‘TRANSMISSION’||’:’||”||ABSTRACT_VEHICLE_DETAILS.TRANSMISSION_TYPE);
DBMS_OUTPUT.PUT_LINE(‘MODALCATEGORY’||’:’||”||ABSTRACT_VEHICLE_DETAILS.END_RANGE);
DBMS_OUTPUT.PUT_LINE(‘TOTALPRICE’||’:’||”||ABSTRACT_VEHICLE_DETAILS.TOTAL_PRICE);
END;
As like in above screenshot, it throws an error as we are trying to instantiate the abstract members of super object type(CAR).
Step 5: Create an anonymous block to execute the abstracted object type through the sub type parameters
Syntax:
DECLARE
<declarative section>
BEGIN
<execution part>
EXCEPTION
<exception part>
END;
Code:
DECLARE
VEHICLE_DETAILS CATEGORY:= CATEGORY (‘HONDA’, ‘AMT’, ‘HIGH’, ‘P’, 1000000);
BEGIN
DBMS_OUTPUT.PUT_LINE (‘BRAND’||’:’||’ ‘||VEHICLE_DETAILS.BRAND_NAME);
DBMS_OUTPUT.PUT_LINE(‘TRANSMISSION’||’:’||’ ‘||VEHICLE_DETAILS.TRANSMISSION_TYPE);
DBMS_OUTPUT.PUT_LINE (‘MODAL CATEGORY’||’:’||’ ‘||VEHICLE_DETAILS.END_RANGE);
DBMS_OUTPUT.PUT_LINE (‘TOTAL PRICE’||’:’||’ ‘||VEHICLE_DETAILS.TOTAL_PRICE);
END;
In the above code, abstract object members has been instantiated using sub type object using method overriding.
Output: