1600 PMP mock questions 1400 CAPM mock questions 800 SCJP 6 mock questions 600 OCAJP 7 mock questions 590 OCPJP 7 mock questions 556 SCWCD 5 mock questions 500 OCEJWCD 6 mock questions pdfDownload (java,struts, hibernet etc)

ORACLE Tutorials

Object-Oriented Principles And Working with Objects in Oracle

Object Oriented Programming
A type of programming in which programmers define not only the data type of a data structure, but also the types of operations (functions) that can be applied to the data structure. In this way, the data structure becomes an object that includes both data and functions. In addition, programmers can create relationships between one object and another. For example, objects can inherit characteristics from other objects.
One of the principal advantages of object-oriented programming techniques over procedural programming techniques is that they enable programmers to create modules that do not need to be changed when a new type of object is added. A programmer can simply create a new object that inherits many of its features from existing objects. This makes object-oriented programs easier to modify.
Object-oriented programming (OOP) is a programming language model organized around "objects" rather than "actions" and data rather than logic. Historically, a program has been viewed as a logical procedure that takes input data, processes it, and produces output data. The programming challenge was seen as how to write the logic, not how to define the data. Object-oriented programming takes the view that what we really care about are the objects we want to manipulate rather than the logic required to manipulate them.
The first step in OOPs is to identify all the objects you want to manipulate and how they relate to each other, an exercise often known as data modeling. Once you've identified an object, you generalize it as a class of objects and define the kind of data it contains and any logic sequences that can manipulate it. Each distinct logic sequence is known as a method. A real instance of a class is called an "object" or, in some environments, an "instance of a class." The object or class instance is what you run in the computer. Its methods provide computer instructions and the class object characteristics provide relevant data. You communicate with objects or they communicate with each other with well-defined interfaces called messages.
The concepts and rules used in object-oriented programming provide these important benefits:
?    The concept of a data class makes it possible to define subclasses of data objects that share some or all of the main class characteristics. Called inheritance, this property of OOP forces a more thorough data analysis, reduces development time, and ensures more accurate coding.
?    Since a class defines only the data it needs to be concerned with, when an instance of that class (an object) is run, the code will not be able to accidentally access other program data. This characteristic of hiding data provides greater system security and avoids unintended data corruption.
?    The definition of a class is re-useable not only by the program for which it is initially created but also by other object-oriented programs (and, for this reason, can be more easily distributed for use in networks).
?    The concept of data classes allows a programmer to create any new data type that is not already defined in the language itself.
What is an Object?
There can be a many definitions of the term object as described in various books. Some of them are as follows:
"An object has state, behavior, and identity; the structure and behavior of similar objects are defined in their common class; the terms instance and object are interchangeable".
?An object is a software bundle of related state and behavior. Software objects are often used to model the real-world objects that you find in everyday life?.
?An object is a concept, abstraction, or thing with well-defined boundaries and meaning for an application. Each object in a system has three characteristics: state, behavior, and identity?.
To sum it up, you can define an Object as a representation of the real world entity, which has certain attributes and performs certain operations. In this methodology of programming the object exists in order to provide certain functionality to the System. Every distinct behavior of the object is called as an operation and every object has distinct operations to perform.
Each and every entity or object possesses certain attributes and every instance of that object stores the values of its attributes. As in case of operations, each object has distinct attributes to maintain. You generally decide on attributes once you have decided as to what operations are anticipated by the particular object.
Oracle Implementation of Objects
Oracle implements Objects through the use of TYPE, defined in a similar way to packages. Unlike packages where the instance of the package is limited to the current session, an instance of an object type can be stored in the database for later use. The definition of the type contains a comma-separated list of attributes/properties, defined in the same way as package variables, and member functions/procedures. If the type contains member functions/procedures, the procedural work for these elements is defined in the TYPE BODY.
One can define the object types as the templates of the real world entities. A user-defined object type is a data structure that holds certain variables known as attributes along with certain procedures or functions that characterize the behavior of the object. Though the variables or the set of attributes are visible to the outside world, i.e. client programs, it is always advised that you use it with the methods that are provided as a part of the object type definition.
Oracle allows you to have two types of Object types:
1.    Persistent Object: A persistent object is an object which is stored in an Oracle database. It may be fetched into the object cache and modified by an OCCI application. The lifetime of a persistent object can exceed that of the application which is accessing it. Once it is created, it remains in the database until it is explicitly deleted. There are two types of persistent objects:
?    A standalone instance is stored in a database table row, and has a unique object identifier. An OCCI application can retrieve a reference to a standalone object, pin the object, and navigate from the pinned object to other related objects. Standalone objects may also be referred to as referenceable objects.
It is also possible to select a referenceable object, in which case you fetch the object by value instead of fetching it by reference.
?    An embedded instance is not stored in a database table row, but rather is embedded within another structure. Examples of embedded objects are objects which are attributes of another object, or objects that exist in an object column of a database table. Embedded objects do not have object identifiers, and OCCI applications cannot get REFs to embedded instances.
Embedded objects may also be referred to as non-referenceable objects or value instances. You may sometimes see them referred to as values, which is not to be confused with scalar data values. The context should make the meaning clear.
2.    A transient object: is an instance of an object type. Its lifetime cannot exceed that of the application. The application can also delete a transient object at any time.
The Structure of an Object Type
The structure of the object type is similar to that of a package. It basically consists of two parts, firstly the specification, secondly the body. The Type specification like package specification consists of all variable declaration, also the member function or procedure definitions. It acts as interface for the type. All the declarations in the type specifications are basically public in implementation.
Incase any member subprogram is declared in the specification it needs to be described in the type body. The implementation of the body is Private.
There are certain restrictions that need to be kept in mind:
1.    Any kind of attribute declaration has to be done before any method declaration.
2.    Incase only attributes are defined for an object, there is no need to create any type body for the same.
3.    No attribute declaration is allowed in the type body.
4.    All declaration that are made in the type specification are public i.e. accessible or visible to the outside world.
5.    The body can contain private definitions of methods.
The Type contains three different kinds of methods. Methods are basically all the procedures and functions declared with in the type. The types of methods are Member, Static or Constructor.
Member methods are the ones, which are bound to the Object instance and are generally referenced using the object instance name as prefix. The Static functions are global functions, which are referenced using the type name and passing the instance name as a parameter.
The Constructor is a special function, which has a similar name as that of the object type and is used to generally instantiate the object attributes. The system implicitly creates a constructor, which takes all the attributes as parameters.
The Syntax for the creation of types is as follows:
Create or Replace Type type_name IS|AS OBJECT
(Attribute1 datatype,
Attribute2 datatype,
[MEMBER|STATIC Procedure| Function1 spec,
MEMBER|STATIC Procedure| Function1 spec,
Not Final |Final );

The objects are created by using the above syntax. There are certain guidelines that you should keep in mind while creating the Types.
1.    The datatype of the attribute could be any Oracle server datatype, except LONG, LONG RAW, NCHAR, NCLOB, NVARCHAR2, ROWID, UROWID, PL/SQL and any types that are described inside any PL/SQL package.
2.    The attributes cannot be initialized during the declaration of the type.
3.    The NOT NULL constraint cannot be applied on an attribute.
4.    The NOT FINAL keyword, indicates that the sub types can be defined from this type. The Final keyword does not permit this feature. The default is FINAL.
Step 1: Create a Type Object by defining the object type.
length NUMBER,
width NUMBER,
area NUMBER,
Step 2: Create the type body.
SELF.length := length;
SELF.width := width;
SELF.area := length * width;

The other way to use the same is to use the type as a column in some simple table declaration like
Create table Dimensions
(Dim_no number, Measures square);
Step 4: Insert Data
Insert into My_square values(Square(10,20));
You use the constructor to insert values. In the second case you can enter values as follows
Insert into Dimensions values (10, square(10,20));
Step 5: View the data
Select * from My_square;
In the second case you can view it in two different formats.
Select * from Dimensions;
Select a.Dim_no, a.Measures.length, a.Measures.width, a.Measures.area from Dimensions a;
The results of the above three statements are as follows:
The update, Delete DML statements would work in a similar manner.

Another Example for Practice:
Create or Replace Type my_emp_typ1 IS Object
(empno number,
ename varchar2(20),
salary number(10,2),
Static Procedure Insert_record (tab_name Varchar2, id number, enm Varchar2, sal number),
Static Procedure Update_Record (tab_name Varchar2, id number, enm Varchar2, sal number),
Static Procedure Delete_Record (tab_name Varchar2, id number),
Member Function Calc_salary return number);

Create or Replace Type body my_emp_typ1 IS
Static Procedure Insert_record (tab_name Varchar2, id number, enm Varchar2, sal number)
str varchar2(1000);
str := 'Insert into ' || tab_name || ' Values (:id, :enm, :sal)';
Execute Immediate str using id, enm, sal;
end Insert_record;

Static Procedure Update_Record (tab_name Varchar2, id number, enm Varchar2, sal number)
str varchar2(1000);
str := 'Update ' || tab_name || ' set ename = ''' || enm ||''' , salary = '||sal || ' where empno = '|| id;
Execute Immediate str;
end update_record;
Static Procedure Delete_Record (tab_name Varchar2, id number)
str varchar2(1000);
str := 'Delete From ' || tab_name || ' where empno = ' || id;
Execute Immediate str;
end delete_record;
Member Function Calc_salary return number
return (salary*0.1 + salary);
end Calc_salary;
The procedures like Insert_Record, Update_Record and Delete_Record have been declared as STATIC as the table name is passed during the execution. Keep into mind that the static functions are called using the type name.
Try finding the solutions to the following set of questions.
1.    Create an object table of the above type.
2.    Once a table is created try inserting records with the following values.

Empno    Ename     Salary
10    Ram    8000
20    Ravi    6000
30    Amit    5000
3.    Update the record for Empno : 20, change the name to John and the salary to 7000.
4.    Delete the record for Empno : 30.
5.    Select the data from the table.
6.    Select the data in addition with the member function Calc_Salary.
1>    Create table My_Employee of My_Emp_typ;

2>    Insert into My_Employee values (10,'Ram', 8000);
Execute My_Emp_typ.Insert_record('My_Employee',30,'Amit',6000);
3>    Update My_Employee set ename='John', salary=7000 where empno=20;
Execute My_emp_typ.Update_record(?My_Employee',20,'John',7000);
4>    Execute My_emp_typ.delete_record('My_Employee',30);
5>    Select * from My_Employee;
6>    Select a.empno, a.ename, a.salary , a.calc_salary() from My_Employee a;
Certain Issues with Objects
1.    Once an object is used to define a table only it's BODY cannot be altered. To alter the type all table references to it must be dropped. This means the type definition should be very stable before it is used in a table.
2.    The Export & Import often have difficulties with Object Types.
3.    The SQL*Plus COPY command does not work with Object Types.
4.    There are a number of issues with database links in conjunction with Object Types. This makes the use of object tables in distributed systems impossible without the use of views to hide the object functionality.
Self Parameter
All the member procedures have an implicit parameter, which is referred as SELF. This variable allows you to access the attributes of the instance without using the qualifiers. This makes it simpler to write the member methods.
Just incase the SELF Parameter is not declared explicitly, it always the first parameter passed to the method and incase of member function, its default mode is IN and INOUT in member procedures.
Create Or Replace Type Student As Object
first_name Varchar2(20),
Last_name Varchar2(20),
Member Function Full_name Return Varchar2);

Create or Replace Type Body Student As
Member Function Full_name Return varchar2
Return (SELF.first_name || ' ' || SELF.Last_name);
end Full_name;
Note: The Static methods do not have any implicit Self Parameter.
Type Inheritance
Type inheritance is an essential new feature in Oracle9i Object-Relational Technology. With type inheritance, extending another user-defined type can create a new user-defined type. The new user-defined type is then a subtype of the supertype from which it extends. The subtype automatically inherits all the attributes and methods defined in the supertype. The subtype can add attributes and methods, and overload or override methods inherited from the supertype. Oracle supports this single type inheritance model, which closely aligns with the ANSI/OSI SQL99 standards.
As discussed earlier, you can decide on whether or not the type you create is able to extend the functionality or not by specifying the keyword Final or Not Final.
For Example you can create a type as person, now this person type can be extended to create another type called as employee or Customer or even Student.
Declaration of Type Person :
Create or Replace Type person as object
( First_name Varchar2(20),
Last_name Varchar2(20),
Age Number,
Address Varchar2(50),
Date_of_birth Date)
Create or Replace Type Emp_Extend Under Person
( Empno Number,
Salary Number(10,2),
Dept_num Number,
Job_id Varchar2(10));

Create or Replace Type Student_Extend Under Person
( Roll_NO Number,
Course Varchar2(10));

Now you can create a table of the object type Student_extend
Create table my_student of student_extend;
If you describe the table, the following is the result.
Data Dictionary Views for Objects
You can view the information about the object in the following Data Dictionary Views:
1.    user_objects
2.    user_types
3.    user_object_tables
4.    user_all_tables
Data Dictionary Views for Object Type Methods
You can view the details of object type methods in the data dictionary views as follows:
1.    user_method_params
2.    user_method_results
3.    user_type_methods
4.    user_source

Reviews and Comments

PMP, CAPM, PMI is a registered certification mark of the Project Management Institute, Inc

Copyright © www.techfaq360.com 2016

About US | Contact US | Privacy Policy | Terms and Conditions  | Website disclaimer  | Cancellation and Refund Policy  | Shipping & Delivery Policy