40
Object-Oriente d Programming (OOP) Lecture No. 5

Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05

Embed Size (px)

Citation preview

Page 1: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05

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

Page 2: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05

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

Page 3: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05

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

Page 4: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05

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

Page 5: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05

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

Page 6: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05

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

Page 7: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05

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

Page 8: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05

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? 

Page 9: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05

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

Page 10: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05

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

Page 11: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05

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

Page 12: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05

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

Page 13: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05

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

Page 14: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05

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”  

Page 15: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05

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

Page 16: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05

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

Page 17: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05

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

Page 18: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05

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

Page 19: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05

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

Page 20: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05

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

Page 21: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05

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

Page 22: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05

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

Page 23: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05

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*

Page 24: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05

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

Page 25: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05

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

Page 26: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05

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

*

*

Page 27: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05

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

*

Page 28: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05

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

Page 29: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05

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

Page 30: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05

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

Page 31: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05

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

Page 32: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05

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

Page 33: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05

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

Page 34: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05

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 

Page 35: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05

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

Page 36: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05

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

Page 37: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05

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*

Page 38: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05

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

Page 39: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05

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

Page 40: Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 05

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