Presentation is loading. Please wait.

Presentation is loading. Please wait.

OO - Lecture 4 Tutorial Review Associations Inheritance of Functions Polymorphism.

Similar presentations


Presentation on theme: "OO - Lecture 4 Tutorial Review Associations Inheritance of Functions Polymorphism."— Presentation transcript:

1 OO - Lecture 4 Tutorial Review Associations Inheritance of Functions Polymorphism

2 EMP-DEPT example Three relations: Department : DEPT Employee : EMP Salary Grade : SALGRADE

3 Dept DeptnoDnameLoc 10AccountingNew York 20ResearchDallas 30SalesChicago 40OperationsBoston GradeLosalHisal 1£700.00£1,200.00 2£1,201.00£1,400.00 3£1,401.00£2,000.00 4£2,001.00£3,000.00 5£3,001.00£99,999.00 Salgrade Table

4 EMP - table ( reduced) EmpnoEnameMgr Sal Deptno 7369SMITH7902 £800.0020 7499ALLEN7698£1,600.0030 7521WARD7698£1,250.0030 7566JONES7839£2,975.0020 7654MARTIN7698£1,250.0030 7698BLAKE7839£2,850.0030 7782CLARK7839£2,450.0010 7788SCOTT7566£3,000.0020 7839KING£5,000.0010 7844TURNER7698£1,500.0030 7876ADAMS7788£1,100.0020 7900JAMES7698 £950.0030 7902FORD7566£3,000.0020 7934MILLER7782£1,300.0010

5 dept Emp salgrade Deptno (Pk) dname loc Empno (Pk) ename job hiredate sal comm mgr (Fk-->emp) deptno (Fk-Dept) manages Losal hisal grade

6 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

7 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

8 Dept Deptno : String Dname : String Location : String Emp Empno : String Ename : String Hiredate : Date Job : String Sal : Money Comm : Money 1 0..* 1

9 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;

10 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

11 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?

12 Use inheritance Contractor hourrate : Money hours : Number Staff Empno : String Ename : String Hiredate : Date Job : String Salaried Sal : Money Comm : Money

13 Inheritance of attributes What attributes has a salaried staff? What attributes has a contractor?

14 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?

15 A member function member function daysWorked return number is begin return hiredate - sysdate; end ;

16 Inheritance of functions Contractor hourrate : Money hours : Number Staff Empno : String Ename : String Hiredate : Date Job : String daysWorked() : Number Salaried Sal : Money Comm : Money

17 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

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

19 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

20 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

21 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?

22 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

23 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

24 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


Download ppt "OO - Lecture 4 Tutorial Review Associations Inheritance of Functions Polymorphism."

Similar presentations


Ads by Google