Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

7/10/2018

Object Oriented Programming


Dr. Muzammil Khan

Assistant Professor
Department of Computer Science

Office E – 12

Chapter 4
Multiple Inheritance

Object Oriented Programming


2

1
7/10/2018

Multiple Inheritance
 For Example
 Mermaid

Woman Fish

Mermaid
Object Oriented Programming
3

Example – Multiple Inheritance


 Amphibious Vehicle

Vehicle

Land Vehicle Water Vehicle

Car Amphibious Vehicle Boat


Object Oriented Programming
4

2
7/10/2018

Problems with Multiple Inheritance

 Increased complexity

 Reduced understanding

 Duplicate features

Object Oriented Programming


5

Problem – Duplicate Features

Woman Fish
eat eat
… …

Mermaid

 Which eat operation Mermaid inherits?

Object Oriented Programming


6

3
7/10/2018

Solution – Override the Common Feature

Woman Fish
eat eat
… …

Mermaid Invoke eat


operation of
eat desired
… class

Object Oriented Programming


7

Problem – Duplicate Features


(Diamond Problem)

Vehicle
changeGear

Land Vehicle Water Vehicle

Car Amphibious Vehicle Boat

 Which changeGear operation Amphibious Vehicle


inherits?
Object Oriented Programming
8

4
7/10/2018

Solution - Diamond Problem

 Some languages disallow diamond hierarchy

 Others provide mechanism to ignore characteristics from


one side

Object Oriented Programming


9

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
 Kinds of Association
 Class Association
 Inheritance
 Object Association
 Simple Association
 Composition
 Aggregation

Object Oriented Programming


10

5
7/10/2018

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”
 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
Object Oriented Programming
11

One-way Association
 We can navigate along a single direction only
 Denoted by an arrow towards the server object
 Examples
lives-in
Ali House
1 1
 Ali lives in a House

lives-in
Ali Car
1 *

 Ali drives his Car


Object Oriented Programming
12

6
7/10/2018

Two-way Association
 We can navigate in both directions
 Denoted by a line between the associated objects
 Example
works-for
Employee Company
* 1
 Employee works for company
 Company employs employees

Friend of
Ali Ahmad
1 1
 Ahmad is a friend of Ali
 Ali is a friend of Ahmad Object Oriented Programming
13

Binary Association
 Associates objects of exactly two classes
 Denoted by a line, or an arrow between the associated
objects
 Examples

works-for
Employee Company
* 1

 Association “works-for” associates objects of exactly two


classes
 Similarly,
 Association “drives” associates objects of exactly two
classes
Object Oriented Programming
14

7
7/10/2018

Ternary Association
 Associates objects of exactly three classes
 Denoted by a diamond with lines connected to associated
objects
 Example

Student 1
Teacher
*

Course

Object Oriented Programming


15

Example – Ternary Association

Project * *
Language

Person

 Objects of exactly three classes are associated

Object Oriented Programming


16

8
7/10/2018

Object 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

Object Oriented Programming


17

Example – Composition of Ali

Head
1

Arm Ali Leg


2 2

1
Body
Object Oriented Programming
18

9
7/10/2018

Example – Composition of Chair


Back
1

Chair

2 1 4
Arm Seat Leg
Object Oriented Programming
19

Stronger Composition
 Composition is a stronger relationship, because
 Composed object becomes a part of the composer
 Composed object can’t exist independently

 Examples
 Ali is made up of different body parts
 They can’t exist independent of Ali

 Similarly,

 Chair’s body is made up of different parts


 They can’t exist independently

Object Oriented Programming


20

10
7/10/2018

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

Object Oriented Programming


21

Example – Aggregation

Bed
1

Chair Room Table


* 1

1
Cupboard
Object Oriented Programming
22

11
7/10/2018

Example – Aggregation
 Similarly

Garden * Plants

 Aggregation is Weaker
 As compared to Composition

Object Oriented Programming


23

Aggregation is Weaker
 Aggregation is weaker relationship, because
 Aggregate object is not a part of the container
 Aggregate object can exist independently

 Examples
 Furniture is not an intrinsic part of room
 Furniture can be shifted to another room, and so can exist
independent of a particular room

 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

Object Oriented Programming


24

12
7/10/2018

End of Chapter
 Lab Work
 Practice programs representing multi-inheritance

 You may have quiz next week

Object Oriented Programming


25

13

You might also like