40
Object-Oriented Object-Oriented Programming (OOP) Programming (OOP) Lecture No. 5 Lecture No. 5

Object-Oriented Programming (OOP) Lecture No. 5. Multiple Inheritance ► We may want to reuse characteristics of more than one parent class

Embed Size (px)

Citation preview

Object-Oriented Object-Oriented Programming (OOP)Programming (OOP)

Lecture No. 5Lecture No. 5

Multiple InheritanceMultiple Inheritance

►We may want to reuse characteristics We may want to reuse characteristics of more than one parent classof more than one parent class

Example – Multiple Example – Multiple InheritanceInheritance

Mermaid

Example – Multiple Example – Multiple InheritanceInheritance

Mermaid

Woman Fish

Example – Multiple Example – Multiple InheritanceInheritance

Amphibious Vehicle

Example – Multiple Example – Multiple InheritanceInheritance

Amphibious Vehicle

Land Vehicle Water Vehicle

Vehicle

Car Boat

Problems with Multiple Problems with Multiple InheritanceInheritance

► Increased complexityIncreased complexity

►Reduced understandingReduced understanding

►Duplicate featuresDuplicate features

Problem – Duplicate FeaturesProblem – Duplicate Features

Mermaid

Woman Fisheat…

eat…

►Which Which eateat operation operation MermaidMermaid inherits? inherits?

Solution – Override the Common Solution – Override the Common FeatureFeature

Mermaid

Woman Fisheat…

eat…

eat…

Invoke eat operation of desired class

Problem – Duplicate Features Problem – Duplicate Features (Diamond Problem)(Diamond Problem)

►Which Which changeGearchangeGear operation operation Amphibious Vehicle inherits?Amphibious Vehicle inherits?

Amphibious Vehicle

Land Vehicle Water Vehicle

Vehicle

Car Boat

changeGear

Solution to Diamond ProblemSolution to Diamond Problem

►Some languages disallow diamond Some languages disallow diamond hierarchyhierarchy

►Others provide mechanism to ignore Others provide mechanism to ignore characteristics from one sidecharacteristics from one side

AssociationAssociation

►Objects in an object model interact Objects in an object model interact with each otherwith each other

►Usually an object provides services to Usually an object provides services to several other objectsseveral other objects

►An object keeps associations with An object keeps associations with other objects to delegate tasksother objects to delegate tasks

Kinds of AssociationKinds of Association

►Class AssociationClass Association InheritanceInheritance

►Object AssociationObject Association Simple AssociationSimple Association CompositionComposition AggregationAggregation

Simple AssociationSimple Association

► Is the weakest link between objectsIs the weakest link between objects

► Is a reference by which one object can Is a reference by which one object can interact with some other objectinteract with some other object

► Is simply called as “association”Is simply called as “association”

Kinds of Simple AssociationKinds of Simple Association

►w.r.t navigationw.r.t navigation One-way AssociationOne-way Association Two-way AssociationTwo-way Association

►w.r.t number of objectsw.r.t number of objects Binary AssociationBinary Association Ternary AssociationTernary Association N-ary AssociationN-ary Association

One-way AssociationOne-way Association

►We can navigate along a single We can navigate along a single direction onlydirection only

►Denoted by an arrow towards the Denoted by an arrow towards the server objectserver object

Example – AssociationExample – Association

►Ali lives in a HouseAli lives in a House

Ali Houselives-in11

Example – AssociationExample – Association

►Ali drives his CarAli drives his Car

Ali Cardrives*1

Two-way AssociationTwo-way Association

►We can navigate in both directionsWe can navigate in both directions

►Denoted by a line between the Denoted by a line between the associated objectsassociated objects

Example – Two-way Example – Two-way AssociationAssociation

Employee Companyworks-for1*

►Employee works for companyEmployee works for company►Company employs employeesCompany employs employees

Example – Two-way Example – Two-way AssociationAssociation

Yasir Alifriend11

►Yasir is a friend of AliYasir is a friend of Ali►Ali is a friend of YasirAli is a friend of Yasir

Binary AssociationBinary Association

►Associates objects of exactly two Associates objects of exactly two classesclasses

►Denoted by a line, or an arrow Denoted by a line, or an arrow between the associated objectsbetween the associated objects

Example – Binary AssociationExample – Binary Association

►Association “works-for” associates Association “works-for” associates objects of exactly two classesobjects of exactly two classes

Employee Companyworks-for1*

Example – Binary AssociationExample – Binary Association

►Association “drives” associates objects Association “drives” associates objects of exactly two classesof exactly two classes

Ali Cardrives*1

Ternary AssociationTernary Association

►Associates objects of exactly three Associates objects of exactly three classesclasses

►Denoted by a diamond with lines Denoted by a diamond with lines connected to associated objectsconnected to associated objects

Example – Ternary Example – Ternary AssociationAssociation

►Objects of exactly three classes are Objects of exactly three classes are associatedassociated

Student Teacher

Course

1

*

*

Example – Ternary Example – Ternary AssociationAssociation

Project Language

Person

►Objects of exactly three classes are Objects of exactly three classes are associatedassociated

*

1

*

N-ary AssociationN-ary Association

►An association between 3 or more An association between 3 or more classesclasses

►Practical examples are very rarePractical examples are very rare

CompositionComposition

►An object may be composed of other An object may be composed of other smaller objectssmaller objects

►The relationship between the “part” The relationship between the “part” objects and the “whole” object is objects and the “whole” object is known as Compositionknown as Composition

►Composition is represented by a line Composition is represented by a line with a filled-diamond head towards the with a filled-diamond head towards the composer objectcomposer object

Example – Composition of AliExample – Composition of Ali

Ali

Body

Arm

Head

Leg

1

1

2 2

Example – Composition of Example – Composition of ChairChair

Chair

SeatArm

Back

Leg

1

12 4

Composition is StrongerComposition is Stronger

►Composition is a stronger relationship, Composition is a stronger relationship, becausebecause Composed object becomes a part of the Composed object becomes a part of the

composercomposer Composed object can’t exist Composed object can’t exist

independentlyindependently

Example – Composition is Example – Composition is StrongerStronger

►Ali is made up of different body partsAli is made up of different body parts

►They can’t exist independent of AliThey can’t exist independent of Ali

Example – Composition is Example – Composition is StrongerStronger

►Chair’s body is made up of different Chair’s body is made up of different partsparts

►They can’t exist independentlyThey can’t exist independently

AggregationAggregation

►An object may contain a collection An object may contain a collection (aggregate) of other objects(aggregate) of other objects

►The relationship between the The relationship between the container and the contained object is container and the contained object is called aggregationcalled aggregation

►Aggregation is represented by a line Aggregation is represented by a line with unfilled-diamond head towards with unfilled-diamond head towards the containerthe container

Example – AggregationExample – Aggregation

Room

Cupboard

Bed

Chair Table*

1

1

1

Example – AggregationExample – Aggregation

Garden Plant*

Aggregation is WeakerAggregation is Weaker

►Aggregation is weaker relationship, Aggregation is weaker relationship, becausebecause Aggregate object is not a part of the Aggregate object is not a part of the

containercontainer Aggregate object can exist independentlyAggregate object can exist independently

Example – Aggregation is Example – Aggregation is WeakerWeaker

►Furniture is not an intrinsic part of Furniture is not an intrinsic part of roomroom

►Furniture can be shifted to another Furniture can be shifted to another room, and so can exist independent of room, and so can exist independent of a particular rooma particular room

Example – Aggregation is Example – Aggregation is WeakerWeaker

►A plant is not an intrinsic part of a A plant is not an intrinsic part of a gardengarden

► It can be planted in some other It can be planted in some other garden, and so can exist independent garden, and so can exist independent of a particular gardenof a particular garden