oo - lecture 4 tutorial review associations inheritance of functions polymorphism

24
OO - Lecture 4 • Tutorial Review • Associations • Inheritance of Functions • Polymorphism

Post on 22-Dec-2015

216 views

Category:

Documents


2 download

TRANSCRIPT

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

OO - Lecture 4

• Tutorial Review

• Associations

• Inheritance of Functions

• Polymorphism

Page 2: OO - Lecture 4 Tutorial Review Associations Inheritance of Functions Polymorphism

EMP-DEPT exampleThree relations:

Department : DEPT

Employee : EMP

Salary Grade : SALGRADE

Page 3: OO - Lecture 4 Tutorial Review Associations Inheritance of Functions Polymorphism

DeptDeptno Dname Loc10 Accounting New York20 Research Dallas30 Sales Chicago40 Operations Boston

Grade Losal Hisal1 £700.00 £1,200.002 £1,201.00 £1,400.003 £1,401.00 £2,000.004 £2,001.00 £3,000.005 £3,001.00 £99,999.00

Salgrade Table

Page 4: OO - Lecture 4 Tutorial Review Associations Inheritance of Functions Polymorphism

EMP - table ( reduced)Empno Ename Mgr Sal Deptno7369 SMITH 7902 £800.00 207499 ALLEN 7698 £1,600.00 307521 WARD 7698 £1,250.00 307566 JONES 7839 £2,975.00 207654 MARTIN 7698 £1,250.00 307698 BLAKE 7839 £2,850.00 307782 CLARK 7839 £2,450.00 107788 SCOTT 7566 £3,000.00 207839 KING £5,000.00 107844 TURNER 7698 £1,500.00 307876 ADAMS 7788 £1,100.00 207900 JAMES 7698 £950.00 307902 FORD 7566 £3,000.00 207934 MILLER 7782 £1,300.00 10

Page 5: OO - Lecture 4 Tutorial Review Associations Inheritance of Functions Polymorphism

dept

Emp

salgrade

Deptno (Pk)dnameloc

Empno (Pk)enamejobhiredatesalcommmgr (Fk-->emp)deptno (Fk-Dept)

manages

Losalhisalgrade

Page 6: OO - Lecture 4 Tutorial Review Associations Inheritance of Functions Polymorphism

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

LatitudeLongitude

emp

dept

Page 7: OO - Lecture 4 Tutorial Review Associations Inheritance of Functions Polymorphism

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

Page 8: OO - Lecture 4 Tutorial Review Associations Inheritance of Functions Polymorphism

Dept

Deptno : StringDname : StringLocation : String

Emp

Empno : StringEname : StringHiredate : DateJob : StringSal : MoneyComm : Money

1

0..*

1

0..*

Page 9: OO - Lecture 4 Tutorial Review Associations Inheritance of Functions Polymorphism

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;

Page 10: OO - Lecture 4 Tutorial Review Associations Inheritance of Functions Polymorphism

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)

A field which contains a reference to another object ( here a dept object)

Get a reference (pointer) to a dept object

Get a reference (pointer) to a dept object

select s.ename,s.deptref.dnamefrom staffs s;

Just follow the reference

Just follow the reference

Page 11: OO - Lecture 4 Tutorial Review Associations Inheritance of Functions Polymorphism

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?

Page 12: OO - Lecture 4 Tutorial Review Associations Inheritance of Functions Polymorphism

Use inheritance

Contractor

hourrate : Moneyhours : Number

Staff

Empno : StringEname : StringHiredate : DateJob : String

Salaried

Sal : MoneyComm : Money

Page 13: OO - Lecture 4 Tutorial Review Associations Inheritance of Functions Polymorphism

Inheritance of attributes

• What attributes has a salaried staff?

• What attributes has a contractor?

Page 14: OO - Lecture 4 Tutorial Review Associations Inheritance of Functions Polymorphism

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?

Page 15: OO - Lecture 4 Tutorial Review Associations Inheritance of Functions Polymorphism

A member function

member function daysWorked return number is begin return hiredate - sysdate;

end;

Page 16: OO - Lecture 4 Tutorial Review Associations Inheritance of Functions Polymorphism

Inheritance of functions

Contractor

hourrate : Moneyhours : Number

Staff

Empno : StringEname : StringHiredate : DateJob : String

daysWorked() : Number

Salaried

Sal : MoneyComm : Money

Page 17: OO - Lecture 4 Tutorial Review Associations Inheritance of Functions Polymorphism

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 staffAttributes from

salaried

Attributes from salaried

Page 18: OO - Lecture 4 Tutorial Review Associations Inheritance of Functions Polymorphism

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.

Page 19: OO - Lecture 4 Tutorial Review Associations Inheritance of Functions Polymorphism

overriding member function annualsal return number is begin return hourrate * hours; end;

In Contractor

Multiple Function definitions

In Salariedoverriding member function annualsal return number is begin return sal*12 + comm; end;

Same function name - different function implementations for each subtype

Page 20: OO - Lecture 4 Tutorial Review Associations Inheritance of Functions Polymorphism

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)

No implementation of this function in Staff (so there must be one in every subtype)

Type can have subtypes

Type can have subtypes

Cannot create instances (objects) of this type - Abstract

Cannot create instances (objects) of this type - Abstract

Page 21: OO - Lecture 4 Tutorial Review Associations Inheritance of Functions Polymorphism

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?

Page 22: OO - Lecture 4 Tutorial Review Associations Inheritance of Functions Polymorphism

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

Some are Salaried , some are Contractors

Appropriate function implementaion is executed

Appropriate function implementaion is executed

s takes on the ‘shape’ of different kinds of Staff

s takes on the ‘shape’ of different kinds of Staff

Select all the staff in the department

Select all the staff in the department

Page 23: OO - Lecture 4 Tutorial Review Associations Inheritance of Functions Polymorphism

Further ExtensionsHow 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

Page 24: OO - Lecture 4 Tutorial Review Associations Inheritance of Functions Polymorphism

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