OO - Lecture 4 Tutorial Review Associations Inheritance of Functions Polymorphism
EMP-DEPT example Three relations: Department : DEPT Employee : EMP Salary Grade : SALGRADE
Dept DeptnoDnameLoc 10AccountingNew York 20ResearchDallas 30SalesChicago 40OperationsBoston GradeLosalHisal 1£700.00£1, £1,201.00£1, £1,401.00£2, £2,001.00£3, £3,001.00£99, Salgrade Table
EMP - table ( reduced) EmpnoEnameMgr Sal Deptno 7369SMITH7902 £ ALLEN7698£1, WARD7698£1, JONES7839£2, MARTIN7698£1, BLAKE7839£2, CLARK7839£2, SCOTT7566£3, KING£5, TURNER7698£1, ADAMS7788£1, JAMES7698 £ FORD7566£3, MILLER7782£1,
dept Emp salgrade Deptno (Pk) dname loc Empno (Pk) ename job hiredate sal comm mgr (Fk-->emp) deptno (Fk-Dept) manages Losal hisal grade
Review AGGREGATION E.g. the Step-function type, or latlong type Each part belongs exclusively to its parent If you delete the parent, you delete all the parts Parts can’t move to another parent How is Dept-Emp different? latlong dm Latitude Longitude emp dept
Associations Departments and employees have independent existence Relationship can be changed - employee can move department If Dept deleted, relationship removed but not the employee. ASSOCIATION
Dept Deptno : String Dname : String Location : String Emp Empno : String Ename : String Hiredate : Date Job : String Sal : Money Comm : Money 1 0..* 1
Foreign key solution In a relational DB, we implement a 1-Many association as : –primary key on DEPT e.g. DEPTNO –column in EMP containing values of DEPTNO To navigate from emp to dept: select ename, dname from emp, dept where emp.deptno=dept.deptno;
Object reference solution create or replace type staff as object ( … deptno number(4), deptref ref dept, … update staffs s set deptref = (select ref(d) from depts d where d.deptno = s.deptno); A field which contains a reference to another object ( here a dept object) Get a reference (pointer) to a dept object select s.ename,s.deptref.dname from staffs s; Just follow the reference
Kinds of Employee Suppose we want to include contractors as well as salaried employees. Contractors have the same basic data but the wages are calculated from an hourly rate and the number of hours worked. How can we represent this variation?
Use inheritance Contractor hourrate : Money hours : Number Staff Empno : String Ename : String Hiredate : Date Job : String Salaried Sal : Money Comm : Money
Inheritance of attributes What attributes has a salaried staff? What attributes has a contractor?
Inheritance of Functions Days worked: –the number of days worked can be calculated by subtracting hiredate from sysdate (todays date) [not quite right !] Where can we place this function? How do we calculate the days worked for a salaried staff? For a contractor?
A member function member function daysWorked return number is begin return hiredate - sysdate; end ;
Inheritance of functions Contractor hourrate : Money hours : Number Staff Empno : String Ename : String Hiredate : Date Job : String daysWorked() : Number Salaried Sal : Money Comm : Money
Inheritance at work Attributes are inherited when an object is constructed (at birth):- –emp staff; –emp:= salaried(7499,'ALLEN','13 jun 93', ‘Analyst’, 1600,300); Functions are inherited dynamically - when called –emp.daysWorked() How? System looks at Salaried type first, but no daysWorked(), so looks in supertype - Staff Attributes from staff Attributes from salaried
Specialisation of functions Each kind of staff needs its own calculation of annualSal –one for salaried using 12*sal + comm –one for contractors using hours * hourrate so provide a different function (with the same name) in each subtype.
overriding member function annualsal return number is begin return hourrate * hours; end; In Contractor Multiple Function definitions In Salaried overriding member function annualsal return number is begin return sal*12 + comm; end; Same function name - different function implementations for each subtype
In Staff (the super type) create or replace type staff as object ( ….. not instantiable member function annualsal return number) not instantiable not final; No implementation of this function in Staff (so there must be one in every subtype) Type can have subtypes Cannot create instances (objects) of this type - Abstract
Polymorphism Consider a function in Dept to calculate the total wage bill (in the department) It will call annualSal() on each of its employees in turn But some are Salaried, some are Contractors?
Polymorphism ‘ many shapes’ member function annualtotal return number is tot number(12,2); sal number(12,2); cursor deptstaff is select s.annualsal() as sal from staffs s where s.deptno = self.deptno; begin tot:=0; for ts in deptstaff loop tot := tot + ts.sal; end loop; return tot; end; Some are Salaried, some are Contractors Appropriate function implementaion is executed s takes on the ‘shape’ of different kinds of Staff Select all the staff in the department
Further Extensions How would you do this? Add children to staff –each staff member can have up to 20 children, recording just their names and dob Implement the salary table Add another type of staff - volunteer staff are paid a simple annual gratuity
Next Week Tutorial –Work with tutor on the Emp-Dept case study and its implementation –Understand what has been done so far –Add some extensions Lecture –Processes UML diagrams Scenarios Process models