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 et...

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 s...

Aggregate Functions and Group By Clause in Oracle with Examples #Diksha Oracle Training Centre

  Aggregate Functions and Group By Clause Oracle with Examples.   Hello Everyone, Today I will   discuss about   Aggregate function in SQL by using Group By Clause and different clauses with some examples . Please go tjrough my previous blogs in Archive folder for classification of SQL, Commands and   SQL joins for your understanding.   Aggregate Functions Allows us to perform a calculation on a set of values to return a single value . We can use Group by Clause to group the result-set by one or more columns. Also we can use Having clause to restrict or filter the data as per our requirement. Note: Whenever we use Aggregate function in SQL we can’t able to use where condition. To restrict or filter the record we need to use having clause instead of Where. Below is the most commonly used Aggregate function in SQL.   MAX : Max function   used to get the maximum values in a set of values. COUNT : This function used to count rows in ...