Object-Oriente d Programming (OOP) Lecture No. 5
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-05 1/40
Object-Oriented Programming(OOP)
Lecture No. 5
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-05 2/40
Multiple Inheritance
►We may want to reuse characteristics ofmore than one parent class
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-05 3/40
Example – Multiple Inheritance
Mermaid
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-05 4/40
Example – Multiple Inheritance
Mermaid
Woman Fish
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-05 5/40
Example – Multiple Inheritance
Amphibious Vehicle
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-05 6/40
Example – Multiple Inheritance
Amphibious Vehicle
Land Vehicle Water Vehicle
Vehicle
Car Boat
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-05 7/40
Problems with MultipleInheritance
►Increased complexity
►Reduced understanding
►Duplicate features
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-05 8/40
Problem – Duplicate Features
Mermaid
Woman Fisheat
…
eat
…
►Which eat operation Mermaid inherits?
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-05 9/40
Solution – Override the CommonFeature
Mermaid
Woman Fisheat
…
eat
…
eat
…
Invoke eatoperation of
desired class
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-05 10/40
Problem – Duplicate Features(Diamond Problem)
►Which changeGear operation Amphibious
Vehicle inherits?
Amphibious Vehicle
Land Vehicle Water Vehicle
Vehicle
Car Boat
changeGear
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-05 11/40
Solution to Diamond Problem
►Some languages disallow diamond
hierarchy
►Others provide mechanism to ignore
characteristics from one side
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-05 12/40
Association
►Objects in an object model interact witheach other
►Usually an object provides services toseveral other objects
► An object keeps associations with otherobjects to delegate tasks
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-05 13/40
Kinds of Association
►Class Association
Inheritance
►Object Association
Simple Association
Composition Aggregation
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-05 14/40
Simple Association
►Is the weakest link between objects
►Is a reference by which one object caninteract with some other object
►Is simply called as “association”
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-05 15/40
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
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-05 16/40
One-way Association
►We can navigate along a single directiononly
►Denoted by an arrow towards the server
object
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-05 17/40
Example – Association
► Ali lives in a House
Ali House
lives-in
11
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-05 18/40
Example – Association
► Ali drives his Car
Ali Car
drives
*1
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-05 19/40
Two-way Association
►We can navigate in both directions
►Denoted by a line between the associatedobjects
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-05 20/40
Example – Two-way Association
Employee Company
works-for
1*
►Employee works for company►Company employs employees
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-05 21/40
Example – Two-way Association
Yasir Alifriend
11
► Yasir is a friend of Ali► Ali is a friend of Yasir
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-05 22/40
Binary Association
► Associates objects of exactly two classes
►Denoted by a line, or an arrow between theassociated objects
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-05 23/40
Example – Binary Association
► Association “works-for” associates objects ofexactly two classes
Employee Companyworks-for
1*
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-05 24/40
Example – Binary Association
► Association “drives” associates objects ofexactly two classes
Ali Cardrives
*1
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-05 25/40
Ternary Association
► Associates objects of exactly three classes
►Denoted by a diamond with lines connectedto associated objects
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-05 26/40
Example – Ternary Association
►Objects of exactly three classes areassociated
Student Teacher
Course
1
*
*
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-05 27/40
Example – Ternary Association
Project Language
Person
►Objects of exactly three classes areassociated
*
1
*
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-05 28/40
N-ary Association
► An association between 3 or more classes
►Practical examples are very rare
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-05 29/40
Composition
► An object may be composed of othersmaller objects
►The relationship between the “part” objectsand the “whole” object is known asComposition
►Composition is represented by a line with a
filled-diamond head towards the composerobject
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-05 30/40
Example – Composition of Ali
Ali
Body
Arm
Head
Leg
1
1
2 2
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-05 31/40
Example – Composition of Chair
Chair
Seat Arm
Back
Leg
1
12 4
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-05 32/40
Composition is Stronger
►Composition is a stronger relationship,because
Composed object becomes a part of thecomposer
Composed object can’t exist independently
l
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-05 33/40
Example – Composition isStronger
► Ali is made up of different body parts
►They can’t exist independent of Ali
l
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-05 34/40
Example – Composition isStronger
►Chair’s body is made up of different parts
►They can’t exist independently
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-05 35/40
Aggregation
► An object may contain a collection(aggregate) of other objects
►The relationship between the container andthe contained object is called aggregation
► Aggregation is represented by a line withunfilled-diamond head towards the
container
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-05 36/40
Example – Aggregation
Room
Cupboard
Bed
Chair Table*
1
1
1
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-05 37/40
Example – Aggregation
Garden Plant*
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-05 38/40
Aggregation is Weaker
► Aggregation is weaker relationship, because
Aggregate object is not a part of the container
Aggregate object can exist independently
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-05 39/40
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 particularroom
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05
http://slidepdf.com/reader/full/object-oriented-programming-oop-cs304-power-point-slides-lecture-05 40/40
Example – Aggregation is Weaker
► A plant is not an intrinsic part of a garden
►It can be planted in some other garden, andso can exist independent of a particulargarden