Skip to main content

Object-Oriented Programming Concept and Oracle PL/SQL #Diksha Oracle Training Centre

 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.

 

Comments

Post a Comment

Popular posts from this blog

SQL and Classification of SQL in Oracle Database #Diksha Oracle Training Centre

  SQL and Classification of SQL in Oracle Database.   SQL is Structured Query Language , which is used for storing, manipulating and retrieving data stored in a relational database .SQL is the standard language for RDBMS. All the Relational Database Management Systems (RDMS) like Oracle, MySQL, Sybase, Informix, IBM DB2 and Microsoft SQL Server use SQL as their standard database language. Oracle is one of the more secured database as compared to other databases. Importance of   SQL : SQL and PL/SQL is a backend process where all data is stored and retrieved in GUI which created either by any programming languages like Java, C++, PHP etc. so we need to have very secure database so that there will be no impact for users. SQL allows users to access data in the relational database management systems. SQL is used to communicate with a database.SQL and PL/SQL allows users to create and drop databases tables , views , stored procedures , functions , packages , trigger etc. SQL allows

Materialized View uses and Benefits in Database #DOTC_Mdb

Materialized View uses and Benefits in Database. Hello Everyone, Today we will discuss about Materialized view as it’s play important role in database. We already discussed about Simple Views and complex views in my previous blog. Before Materialized view go through my previous blog which related to simple view. https://dheeraj60.blogspot.com/2020/05/benefits-of-creating-and-using-view-in.html As we know View is not a database object and not like table which is stored in database, but view can be created from base table a view is an SQL statement that’s stored in the database. This statement, or view, has a name.A view looks and acts a lot like a table. It has columns and rows, and can be included in SELECT queries just like a table. In other word we can say View is a virtual/logical table which is basically used for security purpose. Let’s understand   about   Materialized view : A materialized view is a view that stores the results of the view’s query. Whenever you query the ma

Top 50 Interview Questions On SQL and PL/SQL #DOTC_Mdb

                    Top 50 Interview Questions On SQL and PL/SQL. Today we will Discuss Top 50 interview questions and answers of SQL and PL/SQL which is frequently asked in interview.     Question 1: What is SQL and Classification of SQL? Answer SQL is a Structure Query Language which is vastly used in RDBMS database like Oracle, Sybase, DB2 , Microsoft SQL server etc.   Classification of SQL is as below: DDL (Data Definition Language):  Commands are  create , alter , drop , truncate etc DML (Data Manipulation Language) : Commands are  insert,update and delete . TCL (Transaction Control Language ) : Commands are  Commit , Rollback and Save point. DCL (Data Control Language) : Commands are Grant , Revoke Question 2:    What is meant by Joins? What are the types of join? Answer Joins are basically used to extract/get data from multiple tables using some common columns or conditions and also by using alias to fulfill the condition.   There are various types of Joins as li