0% found this document useful (0 votes)
19 views7 pages

Lab 10 08122023 040046pm

The document discusses inheritance, method overriding, and abstraction in object-oriented databases. It provides examples of creating base and child object types where the child types inherit and override methods from the base types. It also demonstrates abstraction by hiding implementation details behind package and object type procedures and functions.

Uploaded by

iam.owaisbutt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views7 pages

Lab 10 08122023 040046pm

The document discusses inheritance, method overriding, and abstraction in object-oriented databases. It provides examples of creating base and child object types where the child types inherit and override methods from the base types. It also demonstrates abstraction by hiding implementation details behind package and object type procedures and functions.

Uploaded by

iam.owaisbutt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

ITC-327: Database Administration & Management

Semester (BSIT-07) (Fall 2023)

Lab 10: Inheritance, Method Overloading and Abstraction

Objective(s) :
• To learn inheritance , abstraction and method over riding in object.

1. Inheritance
➢ Object types offer inheritance. This means that we can declare an object type to be “under”
another type, and it inherits all the attributes and methods of that super type or base type.

Example:

Base type (Parent Class) Specification


create or replace type vehicle
as object
(
/*all attributes are comma separated*/
reg_no number,
make varchar2(25),
model date,
member function print return varchar2
)
not instantiable not final;

/* NOT FINAL represents the Base Type. Means Subtype can be created.
NOT INSTANTIABLE means that none of the instance of the Base Type can be created*/
/

Base Type (Parent Class) Body


create or replace type body vehicle
as
/* Type Body has only END without a BEGIN keyword.
Every method has its own BEGIN and END */
member function print return varchar2
is
begin
return 'this is vehicle';
end;
end;
/
Departemnent of Computer Sciences 2/7 Semester Fall 2021
ITC-327: Database Administration & Management Lab 10: Ineritance, Method Overriding and Abstraction

Sub type (Child Class) Specification


create or replace type car under vehicle
(
/* In Sub Type specification, there is no AS OBJECT clause. There is a keyword UNDER before the
BASE TYPE */
price number,
color varchar2(25),
member function sub_print return varchar2
);
/

Sub type (Child Class) Body


create or replace type body car
as
member function sub_print return varchar2
is
begin
return 'this is toyota';
end;
end;
/

Type Instantiation and Calling of Member Methods


DECLARE
v_car car := car(100, 'toyota', '1-Jan-2017', 1800000, 'Black');
/* v_car is an instance of sub_type Car. Its attributes has been initialized with the CONSTRUCTOR
method */
v_message1 Varchar2(100);
v_message2 Varchar2(100);
BEGIN
v_message1 := v_car.print;
v_message2 := v_car.sub_print;
dbms_output.put_line(v_message1);
dbms_output.put_line(v_message2);
END;
/
Departemnent of Computer Sciences 3/7 Semester Fall 2021
ITC-327: Database Administration & Management Lab 10: Ineritance, Method Overriding and Abstraction

2. Method Overriding

➢ Method Overriding is when methods have same prototype in base class as well as derived
class.
➢ Method Overriding occurs when one class is inherited from another class.
➢ Method Overriding is the ability of the inherited class (sub type) rewriting the virtual
method of the base class (base object type).

Example:
Base type (Parent Class) Specification
create or replace type vehicle
as object
(
/*all attributes are comma separated*/
reg_no number,
make varchar2(25),
model date,
member function print return varchar2
)
/* NOT FINAL represents the Base Type. Means Subtype can be created.
NOT INSTANTIABLE means that none of the instance of the Base Type can be created*/
not instantiable not final;

Base Type (Parent Class) Body


create or replace type body vehicle
as
/* Type Body has only END without a BEGIN keyword.
Every method has its own BEGIN and END */
member function print return varchar2
is
begin
return 'this is vehicle';
end;
end;

Sub type (Child Class) Specification with Method Overriding


create or replace type car under vehicle
(
/* In Sub Type specification, there is no AS OBJECT clause. There is a keyword UNDER before the
BASE TYPE */
price number,
color varchar2(25),
/* OVERRIDING keyword is used for the OVERRIDING MEMBER METHODS */
overriding member function print return varchar2
);
/
Departemnent of Computer Sciences 4/7 Semester Fall 2021
ITC-327: Database Administration & Management Lab 10: Ineritance, Method Overriding and Abstraction

Sub type (Child Class) Body with method Overriding


create or replace type body car
as
overriding member function print return varchar2
is
begin
return 'this is toyota';
end;
end;
/

Calling similar methods from base type and subtype

DECLARE
v_car car := car(100, 'toyota', '1-Jan-2017', 1800000, 'Black');
/* v_car is an instance of sub_type Car. Its attributes has been initialized with the CONSTRUCTOR
method */
v_message1 Varchar2(100);
v_message2 Varchar2(100);
BEGIN
v_message1 := (v_car as vehicle).print;
v_message2 := v_car.sub_print;
dbms_output.put_line(v_message1);
dbms_output.put_line(v_message2);
END;
/

3. Abstraction

➢ Abstraction is one of the most essential and important feature of object oriented databases.
➢ Abstraction means displaying only essential information and hiding the details.

Example:
Create a package MY_PACKAGE having a public procedure MUL_TABLE

create or replace package my_package


as
procedure mul_table(a in number);
end;
/

Define the package body of package MY_PACKAGE

create or replace package body my_package


as
procedure mul_table(a in number)
is
begin
for b in 1..10
loop
dbms_output.put_line(a || 'x' || b || '=' || a*b);
end loop;
Departemnent of Computer Sciences 5/7 Semester Fall 2021
ITC-327: Database Administration & Management Lab 10: Ineritance, Method Overriding and Abstraction

end;
end my_package;

Create an object type MY_TYPE having a member procedure TIMES_TABLE

create or replace type my_type


as object
(
x number,
member procedure times_table(x in number)
);

Create body of object type MY_TYPE

create or replace type body my_type


as
member procedure times_table(x in number)
is
begin
my_package.mul_table(x);
end;
end;

Call the member method of object type MY_TYPE

declare
my_obj my_type:=my_type(2);
begin
my_obj.times_table(2);
end;
Departemnent of Computer Sciences 6/7 Semester Fall 2021
ITC-327: Database Administration & Management Lab 10: Ineritance, Method Overriding and Abstraction

Exercises

Library
ISBN_No
Display member function Title

Books Magazines

Author Mag_no

Subject Mag_month
Book_Display member function

Mag_Display member function

INHERITANCE

1. Create a base object type LIBRARY with following attributes and member function
Attributes:
ISBN_no number
Title varchar2(35)

Member Function:
DISPLAY function must display ISBN_no and title of book/magazine.

2. Create a sub object type BOOKS under base type LIBRARY with following attributes and
overriding member function
Attributes:
Author varchar2(25)
Subject varchar2(25)

Overriding Member Function:


DISPLAY overriding function must display Authod and Subject of book.

3. Create a sub object type MAGAZINES under base type LIBRARY with following attributes
and overriding member function
Attributes:
Mag_no number
Month varchar2(20)

Overriding Member Function:


DISPLAY overriding function must display Mag_no and Month of magazine.
4. Write an anonymous block to create instances of a BOOK and a MAGAZINE.
The anonymous block must print the ISBN_No and TITLE of the book and
magazine through DISPLAY function of the base type LIBRARY.

Book’s AUTHOR and its SUBJECT must be displayed through its own
DISPLAY member function.
Similarly Magazine’s MAG_NO and MONTH must be displayed through its
own DISPLAY member function.

ABSTRACTION

5. Using table EMP, create a package which has a public function TOTAL_SAL
and public procedure DISPLAY.
TOTAL_SAL function must take DEPTNO as an input parameter and must
display the total salary of all the managers of that department.
DISPLAY procedure must take MANAGER NO as input parameter and must
display all the employees’ names working under that manager.

6. Create an object type EMPLOYEE having attributes DEPTNO and


MANAGER_NO. EMPLOYEE object type will have member procedures
DISPLAY_SAL and DISPLAY_EMPLOYEE.

DISPLAY_SAL procedure will use EMPLOYEE member attribute DEPTNO


as an input parameter and will display the total salary of all managers of that
DEPTNO by calling package procedure TOTAL_SAL.

DISPLAY_EMPLOYEE procedure will use EMPLOYEE member attribute


MANAGER_NO as an input parameter and will display the all the employees
name working under that manager by calling package procedure
DISPLAY_EMPLOYEE.

7. Write an anonymous block to create an object of type EMPLOYEE and


initialize it with DEPTNO= 10 and MANAGER_NO=7369.
Now call the member procedures of the object to display the total salary of
all manager of DEPTNO = 10 and all employees’ names working under
MANAGER_NO=7369.

You might also like