create or replace type Employee_Type as Object ( empno number(5), ename varchar2(20), bs number(5), member function GetSalary return number, member function GetSalary(ndays number) return number ); create or replace type body Employee_Type as member function getsalary return number is begin return bs; end; member function getsalary(ndays number) return number is begin return bs / 30 * ndays; end; end;
declare e employee_type; begin e := employee_type(101,'Praneeth',10000); -- get total salary dbms_output.put_line( e.getsalary); -- get salary for 25 days dbms_output.put_line( e.getsalary(25)); end;
create or replace type account_type as object ( ano number(10), customer varchar2(30), curbal number(10), static function GetMinBal(atype varchar2) return number ); create or replace type body account_type as static function GetMinBal(atype varchar2) return number is begin if atype = 'S' then return 1000; else return 2000; end if; end; end;
Begin dbms_output.put_line ( account_type.getminbal('C')); end;
create or replace type student_type as object ( rollno number(5), name varchar2(30) );
create table students of student_type;
alter type student_type add attribute( course varchar2(5), feepaid number(6) ) cascade;
SQL> desc student_type Name Null? Type ----------------------------------------- -------- ---------------------------- ROLLNO NUMBER(5) NAME VARCHAR2(30) COURSE VARCHAR2(5) FEEpaid NUMBER(6)
Then let us see how to add a method to an existing object type. Say, we have to add a method that returns the course fee based on the course. The following ALTER TYPE followed by CREATE TYPE BODY command will achieve it.
alter type student_type add member function GetFee return number cascade; create or replace type body student_type as member function GetFee return number is begin return case course when 'Ora' then 10000 when 'ODba' then 20000 when 'Vb' then 12000 end; end; end;
SQL>select name,course, value(s).getfee() totalfee, fee from students s; NAME COURS TOTALFEE FEE ------------------------------ ----- ---------- ---------- Srikanth ODba 20000 15000 Praneeth Ora 10000 5000
create or replace type person_type as object ( name varchar2(20), address varchar2(100), age number(3), final member function GetType return varchar2 ) NOT FINAL; create or replace type body person_type as member function GetType return varchar2 is begin if age < 15 then return 'Child'; elsif age < 25 then return 'Teens'; elsif age < 45 then return 'Middleaged'; else return 'Old'; end if; end; end;
create or replace type student_type under person_type ( course varchar2(20), balance number(5), member procedure payment(amt number), overriding member function ToString return varchar2 ) NOT FINAL; create or replace type body student_type as member procedure payment(amt number) is begin balance := balance - amt; end; overriding member function ToString return varchar2 is begin return name || ':' || to_char(age) || ':' || course || ':' || to_char(balance); end; end;
declare p person_type; s student_type; begin p := person_type('Larry Ellison','California', 50); s := student_type('Alen king','Newyork',22,'MS CS',10000); dbms_output.put_line( p.gettype); dbms_output.put_line( s.gettype); dbms_output.put_line( p.tostring); dbms_output.put_line( s.tostring); s.payment(2000); dbms_output.put_line( s.tostring); end;
Old Teens Larry Ellison:50 Alen king:22:MS CS:10000 Alen king:22:MS CS:8000
declare p person_type; begin p := person_type('Larry Ellison','California', 50); dbms_output.put_line( p.tostring); p := student_type('Alen king','Newyork',22,'MS CS',10000); dbms_output.put_line( p.tostring); end;
1 declare 2 p person_type; 3 begin 4 p := student_type('Alen king','Newyork',22,'MS CS',10000); 5 dbms_output.put_line( p.tostring); 6 p.payment(2000); 7* end; 8 / p.payment(2000); * ERROR at line 6: ORA-06550: line 6, column 6: PLS-00302: component 'PAYMENT' must be declared ORA-06550: line 6, column 4: PL/SQL: Statement ignored
create or replace type employee_type as object ( empno number(5), ename varchar2(20), NOT INSTANTIABLE member function GetSalary return number ) NOT INSTANTIABLE NOT FINAL;
create or replace type tempemp_type UNDER employee_type ( nohours number(3), hourrate number(4), overriding member function GetSalary return number ); create or replace type body tempemp_type as overriding member function GetSalary return number as begin return nohours * hourrate; end; end;
declare e1 tempemp_type; begin e1 := tempemp_type(1,'xyz',10,20); dbms_output.put_line( e1.getsalary ); end;
SQL> declare 2 e1 employee_type; 3 begin 4 e1 := employee_type(1,'xyz'); 5 end; 6 / e1 := employee_type(1,'xyz'); * ERROR at line 4: ORA-06550: line 4, column 12: PLS-00713: attempting to instantiate a type that is NOT INSTANTIABLE ORA-06550: line 4, column 6: PL/SQL: Statement ignored
SQL> declare 2 e1 employee_type; 3 begin 4 e1 := tempemp_type(1,'xyz',20,100); 5 dbms_output.put_line( e1.getsalary()); 6 end; 7 / 2000 PL/SQL procedure successfully completed.