Object-Oriented Programming Concept and Oracle PL/SQL
Hello Everyone,
Today I will
discuss about the concept of Object-Oriented programming Concept. In object
Oriented Programming (OOP) An Object is the key component of Object-oriented
programming. An Object may contain data (fields or variables) or code (methods
or procedures). The creation of these objects is based on a Class.
There are
four Types of Principle in Object Oriented Programming.
1. Inheritance:
Inheritance is most important feature of OOP as it inherit the property of
classes and object . In other word we can say Inheritance is the process of
creating new classes from base classes where there is a relationship exists.
The derived class extends from the base class in order to inherit its
properties. Inheritance can be used for reusability purpose.
2.
Polymorphism : Polymorphism means data into different form. Polymorphism is the
ability of an object to take on many forms. For example : Branch,
Branch_details, transaction classes can
have the same parent class Bank, but each class can have its own method .
3. Data
Abstraction : Data Abstraction in Object
Oriented Programming means displaying only essential information of an object and
hiding the details .
4. Data Encapsulation:
Encapsulation in OOP is very important as it’s hide the information from the
user also it refers to binding the data
and the methods to manipulate that data together in a single unit .
In one line
we can say Object-Oriented Programming is especially suited for building
reusable components and complex applications. This concept allows the
programmer to populate and manipulate the details at object entities level.
Above OOP
concept follows by many Programming Language such as Java , C++ , Python etc.
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.
In Oracle we
can create Type to see the usage of OOP in PL/SQL program.
First we
need to create Object Type and Object body in PL/SQL block then we Create an anonymous block to call
created object type through implicit constructor for any attribute :
Below is the
simple Program for Inheritance and method, here we add the number of characters
in the Name, food and Made in attributes and multiply it by 20.
CREATE OR
REPLACE TYPE FOOD_ITEM AS OBJECT
(
NAME
VARCHAR2(100),
FOOD
VARCHAR2(50),
MADE_IN
VARCHAR(100),
MEMBER
FUNCTION PRICE RETURN NUMBER
)
NOT FINAL;
/
CREATE OR
REPLACE TYPE BODY FOOD_ITEM
IS
MEMBER
FUNCTION PRICE
RETURN
NUMBER
IS
BEGIN
DBMS_OUTPUT.PUT_LINE('PRICE
OF FOOD ITEM');
RETURN (LENGTH (SELF.NAME)
+ LENGTH (SELF.FOOD)
+ LENGTH (MADE_IN))
*20;
END;
END;
/
DECLARE
V_FOOD
FOOD_ITEM :=FOOD_ITEM ('CHICKEN' ,'CHICKEN CURRY','INDIA');
BEGIN
DBMS_OUTPUT.PUT_LINE('
FOOD ITEMS FAMOUS IN INDIA ?');
DBMS_OUTPUT.PUT_LINE('THE
PRICE OF ' ||V_FOOD.MADE_IN || ' = ' ||V_FOOD.PRICE());
END;
/
Note : In above Program SELF keyword MEMBER methods accept a built-in
parameter named SELF, which is an instance of the object type. It is always the
first parameter passed to a MEMBER method. This is passed in subprogram. Here,
I sum the lengths (number of characters) of the three attributes and multiply
it by 20.
Below is the
output of above program for your reference.
Note: In
Oracle an Object type cannot be created at subprogram level, They can be
created only at the schema level. Once the object type is defined in the
schema, then the same can be used in subprograms. The object type can be
created using 'CREATE TYPE'. The type body can be created only after creating
its object type.
PL/SQL
object type contains mainly two components.
Attributes: Attributes
are the column or field in which data are stored. Each attribute will be mapped
to the data type that defines the processing and storage type for that
attribute.
Members/Methods:
Members or Methods are subprograms that are defined in the object type. They
are not used to store any data but mainly used to define process inside the
object type.
In PL/SQL
you can write multiple programs to implement OOPS concept and inherit the
property and also hide the information from the user by using Oracle Package. In
coming blog I will come up with more OOPS programs with examples as I haven’t
covered Stored Procedures and Packages in my previous blog.
Please go
through this blog carefully as OOP concept is very important in other
programming language as well like Java, C++ , Python etc. In coming blogs I will
come up with more examples in PL/SQL related to OOP concept. This blog is
important for candidate who is preparing for interview and good for IT
graduates. Please let me know if you have any doubts.
Thanks.
I think it is very helpful blog.
ReplyDeleteThank you ever so for you article. Really Cool.
ReplyDeletejava training
java online training