Object-Oriented PLSQL ObjectOriented
Object-Oriented PLSQL ObjectOriented
Object-Oriented PL/SQL. PL/SQL allows defining an object type, which helps in designing
object-oriented database in Oracle. An object type allows you to create composite types. Using
objects allow you to implement real world objects with specific structure of data and methods for
operating it. Objects have attributes and methods. Attributes are properties of an object and are
used for storing an object's state; and methods are used for modeling its behavior.
Objects are created using the CREATE [OR REPLACE] TYPE statement. Following is an
example to create a simple address object consisting of few attributes
When the above code is executed at the SQL prompt, it produces the following result
Type created.
Let's create one more object customer where we will wrap attributes and methods together to
have object-oriented feeling
When the above code is executed at the SQL prompt, it produces the following result
Type created.
Instantiating an Object
1
Defining an object type provides a blueprint for the object. To use this object, you need to create
instances of this object. You can access the attributes and methods of the object using the
instance name and the access operator (.) as follows
DECLARE
residence address;
BEGIN
residence := address('103A', 'M.G.Road', 'Jaipur', 'Rajasthan','201301');
dbms_output.put_line('House No: '|| residence.house_no);
dbms_output.put_line('Street: '|| residence.street);
dbms_output.put_line('City: '|| residence.city);
dbms_output.put_line('State: '|| residence.state);
dbms_output.put_line('Pincode: '|| residence.pincode);
END;
/
When the above code is executed at the SQL prompt, it produces the following result
Member Methods
Member methods are used for manipulating the attributes of the object. You provide the
declaration of a member method while declaring the object type. The object body defines the
code for the member methods. The object body is created using the CREATE TYPE BODY
statement.
Constructors are functions that return a new object as its value. Every object has a system
defined constructor method. The name of the constructor is same as the object type. For example
The comparison methods are used for comparing objects. There are two ways to compare
objects
Map method
The Map method is a function implemented in such a way that its value depends upon the value
of the attributes. For example, for a customer object, if the customer code is same for two
customers, both customers could be the same. So the relationship between these two objects
would depend upon the value of code.
2
Order method
The Order method implements some internal logic for comparing two objects. For example, for
a rectangle object, a rectangle is bigger than another rectangle if both its sides are bigger.
When the above code is executed at the SQL prompt, it produces the following result
Type created.
When the above code is executed at the SQL prompt, it produces the following result
DECLARE
r1 rectangle;
r2 rectangle;
r3 rectangle;
3
inc_factor number := 5;
BEGIN
r1 := rectangle(3, 4);
r2 := rectangle(5, 7);
r3 := r1.enlarge(inc_factor);
r3.display;
IF (r1 > r2) THEN -- calling measure function
r1.display;
ELSE
r2.display;
END IF;
END;
/
When the above code is executed at the SQL prompt, it produces the following result
Length: 8
Width: 9
Length: 5
Width: 7
When the above code is executed at the SQL prompt, it produces the following result
Type created.
4
ELSE
return(-1);
END IF;
END measure;
END;
/
When the above code is executed at the SQL prompt, it produces the following result
DECLARE
r1 rectangle;
r2 rectangle;
BEGIN
r1 := rectangle(23, 44);
r2 := rectangle(15, 17);
r1.display;
r2.display;
IF (r1 > r2) THEN -- calling measure function
r1.display;
ELSE
r2.display;
END IF;
END;
/
When the above code is executed at the SQL prompt, it produces the following result
Length: 23
Width: 44
Length: 15
Width: 17
Length: 23
Width: 44
The following programs illustrate the inheritance in PL/SQL Objects. Let us create another
object named TableTop, this is inherited from the Rectangle object. For this, we need to create
the base rectangle object
5
member function enlarge( inc number) return rectangle,
NOT FINAL member procedure display) NOT FINAL
/
When the above code is executed at the SQL prompt, it produces the following result
Type created.
When the above code is executed at the SQL prompt, it produces the following result
When the above code is executed at the SQL prompt, it produces the following result
Type created.
When the above code is executed at the SQL prompt, it produces the following result
6
Type body created.
DECLARE
t1 tabletop;
t2 tabletop;
BEGIN
t1:= tabletop(20, 10, 'Wood');
t2 := tabletop(50, 30, 'Steel');
t1.display;
t2.display;
END;
/
When the above code is executed at the SQL prompt, it produces the following result
Length: 20
Width: 10
Material: Wood
Length: 50
Width: 30
Material: Steel
For example,
When the above code is executed at the SQL prompt, it produces the following result
Type created.