Download - Lecture05
![Page 1: Lecture05](https://reader035.vdocuments.net/reader035/viewer/2022070322/55910e251a28ab20148b4611/html5/thumbnails/1.jpg)
Object-Oriented Programming (OOP)Lecture No. 5
Downloaded From: www.bsit.zxq.net
![Page 2: Lecture05](https://reader035.vdocuments.net/reader035/viewer/2022070322/55910e251a28ab20148b4611/html5/thumbnails/2.jpg)
Multiple Inheritance
We may want to reuse characteristics of more than one parent class
Downloaded From: www.bsit.zxq.net
![Page 3: Lecture05](https://reader035.vdocuments.net/reader035/viewer/2022070322/55910e251a28ab20148b4611/html5/thumbnails/3.jpg)
Example – Multiple Inheritance
Downloaded From: www.bsit.zxq.net
Mermaid
![Page 4: Lecture05](https://reader035.vdocuments.net/reader035/viewer/2022070322/55910e251a28ab20148b4611/html5/thumbnails/4.jpg)
Example – Multiple Inheritance
Downloaded From: www.bsit.zxq.net
Mermaid
Woman Fish
![Page 5: Lecture05](https://reader035.vdocuments.net/reader035/viewer/2022070322/55910e251a28ab20148b4611/html5/thumbnails/5.jpg)
Example – Multiple Inheritance
Downloaded From: www.bsit.zxq.net
Amphibious Vehicle
![Page 6: Lecture05](https://reader035.vdocuments.net/reader035/viewer/2022070322/55910e251a28ab20148b4611/html5/thumbnails/6.jpg)
Example – Multiple Inheritance
Downloaded From: www.bsit.zxq.net
Amphibious Vehicle
Land Vehicle Water Vehicle
Vehicle
Car Boat
![Page 7: Lecture05](https://reader035.vdocuments.net/reader035/viewer/2022070322/55910e251a28ab20148b4611/html5/thumbnails/7.jpg)
Problems with Multiple Inheritance
Increased complexity
Reduced understanding
Duplicate features
Downloaded From: www.bsit.zxq.net
![Page 8: Lecture05](https://reader035.vdocuments.net/reader035/viewer/2022070322/55910e251a28ab20148b4611/html5/thumbnails/8.jpg)
Problem – Duplicate Features
Which eat operation Mermaid inherits?
Downloaded From: www.bsit.zxq.net
Mermaid
Woman Fisheat…
eat…
![Page 9: Lecture05](https://reader035.vdocuments.net/reader035/viewer/2022070322/55910e251a28ab20148b4611/html5/thumbnails/9.jpg)
Solution – Override the Common Feature
Downloaded From: www.bsit.zxq.net
Mermaid
Woman Fisheat…
eat…
eat…
Invoke eat operation of desired class
![Page 10: Lecture05](https://reader035.vdocuments.net/reader035/viewer/2022070322/55910e251a28ab20148b4611/html5/thumbnails/10.jpg)
Problem – Duplicate Features (Diamond Problem)
Which changeGear operation Amphibious Vehicle inherits?
Downloaded From: www.bsit.zxq.net
Amphibious Vehicle
Land Vehicle Water Vehicle
Vehicle
Car Boat
changeGear
![Page 11: Lecture05](https://reader035.vdocuments.net/reader035/viewer/2022070322/55910e251a28ab20148b4611/html5/thumbnails/11.jpg)
Solution to Diamond Problem
Some languages disallow diamond hierarchy
Others provide mechanism to ignore characteristics from one side
Downloaded From: www.bsit.zxq.net
![Page 12: Lecture05](https://reader035.vdocuments.net/reader035/viewer/2022070322/55910e251a28ab20148b4611/html5/thumbnails/12.jpg)
Association
Objects in an object model interact with each other
Usually an object provides services to several other objects
An object keeps associations with other objects to delegate tasks
Downloaded From: www.bsit.zxq.net
![Page 13: Lecture05](https://reader035.vdocuments.net/reader035/viewer/2022070322/55910e251a28ab20148b4611/html5/thumbnails/13.jpg)
Kinds of Association
Class Association› Inheritance
Object Association› Simple Association› Composition› Aggregation
Downloaded From: www.bsit.zxq.net
![Page 14: Lecture05](https://reader035.vdocuments.net/reader035/viewer/2022070322/55910e251a28ab20148b4611/html5/thumbnails/14.jpg)
Simple Association
Is the weakest link between objects
Is a reference by which one object can interact with some other object
Is simply called as “association”
Downloaded From: www.bsit.zxq.net
![Page 15: Lecture05](https://reader035.vdocuments.net/reader035/viewer/2022070322/55910e251a28ab20148b4611/html5/thumbnails/15.jpg)
Kinds of Simple Association
w.r.t navigation› One-way Association› Two-way Association
w.r.t number of objects› Binary Association› Ternary Association› N-ary Association
Downloaded From: www.bsit.zxq.net
![Page 16: Lecture05](https://reader035.vdocuments.net/reader035/viewer/2022070322/55910e251a28ab20148b4611/html5/thumbnails/16.jpg)
One-way Association
We can navigate along a single direction only
Denoted by an arrow towards the server object
Downloaded From: www.bsit.zxq.net
![Page 17: Lecture05](https://reader035.vdocuments.net/reader035/viewer/2022070322/55910e251a28ab20148b4611/html5/thumbnails/17.jpg)
Example – Association
Ali lives in a House
Downloaded From: www.bsit.zxq.net
Ali Houselives-in11
![Page 18: Lecture05](https://reader035.vdocuments.net/reader035/viewer/2022070322/55910e251a28ab20148b4611/html5/thumbnails/18.jpg)
Example – Association
Ali drives his Car
Downloaded From: www.bsit.zxq.net
Ali Cardrives*1
![Page 19: Lecture05](https://reader035.vdocuments.net/reader035/viewer/2022070322/55910e251a28ab20148b4611/html5/thumbnails/19.jpg)
Two-way Association
We can navigate in both directions
Denoted by a line between the associated objects
Downloaded From: www.bsit.zxq.net
![Page 20: Lecture05](https://reader035.vdocuments.net/reader035/viewer/2022070322/55910e251a28ab20148b4611/html5/thumbnails/20.jpg)
Example – Two-way Association
Employee works for company Company employs employees
Downloaded From: www.bsit.zxq.net
Employee Companyworks-for1*
![Page 21: Lecture05](https://reader035.vdocuments.net/reader035/viewer/2022070322/55910e251a28ab20148b4611/html5/thumbnails/21.jpg)
Example – Two-way Association
Yasir is a friend of Ali Ali is a friend of Yasir
Downloaded From: www.bsit.zxq.net
Yasir Alifriend11
![Page 22: Lecture05](https://reader035.vdocuments.net/reader035/viewer/2022070322/55910e251a28ab20148b4611/html5/thumbnails/22.jpg)
Binary Association
Associates objects of exactly two classes
Denoted by a line, or an arrow between the associated objects
Downloaded From: www.bsit.zxq.net
![Page 23: Lecture05](https://reader035.vdocuments.net/reader035/viewer/2022070322/55910e251a28ab20148b4611/html5/thumbnails/23.jpg)
Example – Binary Association
Association “works-for” associates objects of exactly two classes
Downloaded From: www.bsit.zxq.net
Employee Companyworks-for1*
![Page 24: Lecture05](https://reader035.vdocuments.net/reader035/viewer/2022070322/55910e251a28ab20148b4611/html5/thumbnails/24.jpg)
Example – Binary Association
Association “drives” associates objects of exactly two classes
Downloaded From: www.bsit.zxq.net
Ali Cardrives*1
![Page 25: Lecture05](https://reader035.vdocuments.net/reader035/viewer/2022070322/55910e251a28ab20148b4611/html5/thumbnails/25.jpg)
Ternary Association
Associates objects of exactly three classes
Denoted by a diamond with lines connected to associated objects
Downloaded From: www.bsit.zxq.net
![Page 26: Lecture05](https://reader035.vdocuments.net/reader035/viewer/2022070322/55910e251a28ab20148b4611/html5/thumbnails/26.jpg)
Example – Ternary Association
Objects of exactly three classes are associated
Downloaded From: www.bsit.zxq.net
Student Teacher
Course
1
*
*
![Page 27: Lecture05](https://reader035.vdocuments.net/reader035/viewer/2022070322/55910e251a28ab20148b4611/html5/thumbnails/27.jpg)
Example – Ternary Association
Objects of exactly three classes are associated
Downloaded From: www.bsit.zxq.net
Project Language
Person
*
1
*
![Page 28: Lecture05](https://reader035.vdocuments.net/reader035/viewer/2022070322/55910e251a28ab20148b4611/html5/thumbnails/28.jpg)
N-ary Association
An association between 3 or more classes
Practical examples are very rare
Downloaded From: www.bsit.zxq.net
![Page 29: Lecture05](https://reader035.vdocuments.net/reader035/viewer/2022070322/55910e251a28ab20148b4611/html5/thumbnails/29.jpg)
Composition
An object may be composed of other smaller objects
The relationship between the “part” objects and the “whole” object is known as Composition
Composition is represented by a line with a filled-diamond head towards the composer object
Downloaded From: www.bsit.zxq.net
![Page 30: Lecture05](https://reader035.vdocuments.net/reader035/viewer/2022070322/55910e251a28ab20148b4611/html5/thumbnails/30.jpg)
Example – Composition of Ali
Downloaded From: www.bsit.zxq.net
Ali
Body
Arm
Head
Leg
1
1
2 2
![Page 31: Lecture05](https://reader035.vdocuments.net/reader035/viewer/2022070322/55910e251a28ab20148b4611/html5/thumbnails/31.jpg)
Example – Composition of Chair
Downloaded From: www.bsit.zxq.net
Chair
SeatArm
Back
Leg
1
12 4
![Page 32: Lecture05](https://reader035.vdocuments.net/reader035/viewer/2022070322/55910e251a28ab20148b4611/html5/thumbnails/32.jpg)
Composition is Stronger
Composition is a stronger relationship, because› Composed object becomes a part of the
composer› Composed object can’t exist independently
Downloaded From: www.bsit.zxq.net
![Page 33: Lecture05](https://reader035.vdocuments.net/reader035/viewer/2022070322/55910e251a28ab20148b4611/html5/thumbnails/33.jpg)
Example – Composition is Stronger
Ali is made up of different body parts
They can’t exist independent of Ali
Downloaded From: www.bsit.zxq.net
![Page 34: Lecture05](https://reader035.vdocuments.net/reader035/viewer/2022070322/55910e251a28ab20148b4611/html5/thumbnails/34.jpg)
Example – Composition is Stronger
Chair’s body is made up of different parts
They can’t exist independently
Downloaded From: www.bsit.zxq.net
![Page 35: Lecture05](https://reader035.vdocuments.net/reader035/viewer/2022070322/55910e251a28ab20148b4611/html5/thumbnails/35.jpg)
Aggregation
An object may contain a collection (aggregate) of other objects
The relationship between the container and the contained object is called aggregation
Aggregation is represented by a line with unfilled-diamond head towards the container
Downloaded From: www.bsit.zxq.net
![Page 36: Lecture05](https://reader035.vdocuments.net/reader035/viewer/2022070322/55910e251a28ab20148b4611/html5/thumbnails/36.jpg)
Example – Aggregation
Downloaded From: www.bsit.zxq.net
Room
Cupboard
Bed
Chair Table*
1
1
1
![Page 37: Lecture05](https://reader035.vdocuments.net/reader035/viewer/2022070322/55910e251a28ab20148b4611/html5/thumbnails/37.jpg)
Example – Aggregation
Downloaded From: www.bsit.zxq.net
Garden Plant*
![Page 38: Lecture05](https://reader035.vdocuments.net/reader035/viewer/2022070322/55910e251a28ab20148b4611/html5/thumbnails/38.jpg)
Aggregation is Weaker
Aggregation is weaker relationship, because› Aggregate object is not a part of the
container› Aggregate object can exist independently
Downloaded From: www.bsit.zxq.net
![Page 39: Lecture05](https://reader035.vdocuments.net/reader035/viewer/2022070322/55910e251a28ab20148b4611/html5/thumbnails/39.jpg)
Example – Aggregation is Weaker
Furniture is not an intrinsic part of room
Furniture can be shifted to another room, and so can exist independent of a particular room
Downloaded From: www.bsit.zxq.net
![Page 40: Lecture05](https://reader035.vdocuments.net/reader035/viewer/2022070322/55910e251a28ab20148b4611/html5/thumbnails/40.jpg)
Example – Aggregation is Weaker
A plant is not an intrinsic part of a garden
It can be planted in some other garden, and so can exist independent of a particular garden
Downloaded From: www.bsit.zxq.net