Computer Notes - Multiple Inheritance

Embed Size (px)

Citation preview

  • 8/3/2019 Computer Notes - Multiple Inheritance

    1/40

    Multiple InheritanceMultiple Inheritance

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Multiple Inheritance

    2/40

    Multiple InheritanceMultiple Inheritance

    We may want to reuse characteristics of moreWe may want to reuse characteristics of more

    than one parent classthan one parent class

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Multiple Inheritance

    3/40

    ExampleExample Multiple InheritanceMultiple Inheritance

    Mermaid

  • 8/3/2019 Computer Notes - Multiple Inheritance

    4/40

    ExampleExample Multiple InheritanceMultiple Inheritance

    Mermaid

    Woman Fish

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Multiple Inheritance

    5/40

    ExampleExample Multiple InheritanceMultiple Inheritance

    Amphibious Vehicle

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Multiple Inheritance

    6/40

    ExampleExample Multiple InheritanceMultiple Inheritance

    Amphibious Vehicle

    Land Vehicle Water Vehicle

    Vehicle

    Car Boat

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Multiple Inheritance

    7/40

    Problems with Multiple InheritanceProblems with Multiple Inheritance

    Increased complexityIncreased complexity

    Reduced understandingReduced understanding

    Duplicate featuresDuplicate features

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Multiple Inheritance

    8/40

    ProblemProblem Duplicate FeaturesDuplicate Features

    Mermaid

    Woman Fisheat

    eat

    WhichWhich eateatoperationoperation MermaidMermaidinherits?inherits?

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Multiple Inheritance

    9/40

    SolutionSolution Override the CommonOverride the Common

    FeatureFeature

    Mermaid

    Woman Fisheat

    eat

    eat

    Invoke eatoperation of

    desired class

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Multiple Inheritance

    10/40

    ProblemProblem Duplicate FeaturesDuplicate Features

    (Diamond Problem)(Diamond Problem)

    WhichWhich changeGearchangeGearoperation Amphibiousoperation Amphibious

    Vehicle inherits?Vehicle inherits?

    Amphibious Vehicle

    Land Vehicle Water Vehicle

    Vehicle

    Car Boat

    changeGear

  • 8/3/2019 Computer Notes - Multiple Inheritance

    11/40

    Solution to Diamond ProblemSolution to Diamond Problem

    Some languages disallow diamond hierarchySome languages disallow diamond hierarchy

    Others provide mechanism to ignoreOthers provide mechanism to ignore

    characteristics from one sidecharacteristics from one side

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Multiple Inheritance

    12/40

    AssociationAssociation

    Objects in an object model interact with eachObjects in an object model interact with each

    otherother

    Usually an object provides services to severalUsually an object provides services to severalother objectsother objects

    An object keeps associations with other objectsAn object keeps associations with other objects

    to delegate tasksto delegate tasks

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Multiple Inheritance

    13/40

    Kinds of AssociationKinds of Association

    Class AssociationClass Association

    InheritanceInheritance

    Object AssociationObject Association Simple AssociationSimple Association

    CompositionComposition

    AggregationAggregation

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Multiple Inheritance

    14/40

    Simple AssociationSimple Association

    Is the weakest link between objectsIs the weakest link between objects

    Is a reference by which one object can interactIs a reference by which one object can interactwith some other objectwith some other object

    Is simply called asIs simply called asassociationassociation

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Multiple Inheritance

    15/40

    Kinds of Simple AssociationKinds of Simple Association

    w.r.t navigationw.r.t navigation

    OneOne--way Associationway Association

    TwoTwo--way Associationway Association

    w.r.t number of objectsw.r.t number of objects

    Binary AssociationBinary Association

    Ternary AssociationTernary Association

    NN--aryaryAssociationAssociation

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Multiple Inheritance

    16/40

    OneOne--way Associationway Association

    We can navigate along a single direction onlyWe can navigate along a single direction only

    Denoted by an arrow towards the server objectDenoted by an arrow towards the server object

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Multiple Inheritance

    17/40

    ExampleExampleAssociationAssociation

    Smith lives in a HouseSmith lives in a House

    Smith Houselives-in

    11

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Multiple Inheritance

    18/40

    ExampleExampleAssociationAssociation

    Smith drives his CarSmith drives his Car

    Smith Car drives

    *1

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Multiple Inheritance

    19/40

    TwoTwo--way Associationway Association

    We can navigate in both directionsWe can navigate in both directions

    Denoted by a line between the associatedDenoted by a line between the associatedobjectsobjects

  • 8/3/2019 Computer Notes - Multiple Inheritance

    20/40

    ExampleExample TwoTwo--way Associationway Association

    Employee Companyworks-for

    1*

    Employee works for companyEmployee works for companyCompany employs employeesCompany employs employees

  • 8/3/2019 Computer Notes - Multiple Inheritance

    21/40

    ExampleExample TwoTwo--way Associationway Association

    Movis Smithfriend

    11

    Movis is a friend of SmithMovis is a friend of SmithSmith is a friend of MovisSmith is a friend of Movis

  • 8/3/2019 Computer Notes - Multiple Inheritance

    22/40

    Binary AssociationBinary Association

    Associates objects of exactly two classesAssociates objects of exactly two classes

    Denoted by a line, or an arrow between theDenoted by a line, or an arrow between theassociated objectsassociated objects

  • 8/3/2019 Computer Notes - Multiple Inheritance

    23/40

    ExampleExample Binary AssociationBinary Association

    AssociationAssociationworksworks--forforassociates objects ofassociates objects ofexactly two classesexactly two classes

    Employee Companyworks-for

    1*

  • 8/3/2019 Computer Notes - Multiple Inheritance

    24/40

    ExampleExample Binary AssociationBinary Association

    AssociationAssociationdrivesdrivesassociates objects of exactlyassociates objects of exactlytwo classestwo classes

    Smith Car drives

    *1

  • 8/3/2019 Computer Notes - Multiple Inheritance

    25/40

    Ternary AssociationTernary Association

    Associates objects of exactly three classesAssociates objects of exactly three classes

    Denoted by a diamond with lines connected toDenoted by a diamond with lines connected toassociated objectsassociated objects

  • 8/3/2019 Computer Notes - Multiple Inheritance

    26/40

    ExampleExample Ternary AssociationTernary Association

    Objects of exactly three classes are associatedObjects of exactly three classes are associated

    Student Teacher

    Course

    1

    *

    *

  • 8/3/2019 Computer Notes - Multiple Inheritance

    27/40

    ExampleExample Ternary AssociationTernary Association

    Project Language

    Person

    Objects of exactly three classes are associatedObjects of exactly three classes are associated

    *

    1

    *

  • 8/3/2019 Computer Notes - Multiple Inheritance

    28/40

    NN--aryaryAssociationAssociation

    An association between 3 or more classesAn association between 3 or more classes

    Practical examples are very rarePractical examples are very rare

  • 8/3/2019 Computer Notes - Multiple Inheritance

    29/40

    CompositionCompositionAn object may be composed of other smallerAn object may be composed of other smaller

    objectsobjectsThe relationship between theThe relationship between thepartpartobjects andobjects and

    thethewholewholeobject is known as Compositionobject is known as Composition

    Composition is represented by a line with aComposition is represented by a line with a

    filledfilled--diamond head towards the composerdiamond head towards the composer

    objectobject

  • 8/3/2019 Computer Notes - Multiple Inheritance

    30/40

    ExampleExample Composition of SmithComposition of Smith

    Smith

    Body

    Arm

    Head

    Leg

    1

    1

    2 2

  • 8/3/2019 Computer Notes - Multiple Inheritance

    31/40

    ExampleExample Composition of ChairComposition of Chair

    Chair

    SeatArm

    Back

    Leg

    1

    12 4

  • 8/3/2019 Computer Notes - Multiple Inheritance

    32/40

    Composition is StrongerComposition is Stronger

    Composition is a stronger relationship, becauseComposition is a stronger relationship, because Composed object becomes a part of the composerComposed object becomes a part of the composer

    Composed object canComposed object cant exist independentlyt exist independently

  • 8/3/2019 Computer Notes - Multiple Inheritance

    33/40

    ExampleExample Composition is StrongerComposition is Stronger

    Smith is made up of different body partsSmith is made up of different body parts

    They canThey cant exist independent of Smitht exist independent of Smith

  • 8/3/2019 Computer Notes - Multiple Inheritance

    34/40

    ExampleExample Composition is StrongerComposition is Stronger

    ChairChairs body is made up of different partss body is made up of different parts

    They canThey cant exist independentlyt exist independently

  • 8/3/2019 Computer Notes - Multiple Inheritance

    35/40

    AggregationAggregationAn object may contain a collection (aggregate)An object may contain a collection (aggregate)

    of other objectsof other objectsThe relationship between the container and theThe relationship between the container and the

    contained object is called aggregationcontained object is called aggregation

    Aggregation is represented by a line withAggregation is represented by a line with

    unfilledunfilled--diamond head towards the containerdiamond head towards the container

  • 8/3/2019 Computer Notes - Multiple Inheritance

    36/40

    ExampleExampleAggregationAggregation

    Room

    Cupboard

    Bed

    Chair Table*

    1

    1

    1

  • 8/3/2019 Computer Notes - Multiple Inheritance

    37/40

    ExampleExampleAggregationAggregation

    Garden Plant*

  • 8/3/2019 Computer Notes - Multiple Inheritance

    38/40

    Aggregation is WeakerAggregation is Weaker

    Aggregation is weaker relationship, becauseAggregation is weaker relationship, becauseAggregate object is not a part of the containerAggregate object is not a part of the container

    Aggregate object can exist independentlyAggregate object can exist independently

  • 8/3/2019 Computer Notes - Multiple Inheritance

    39/40

    ExampleExampleAggregation is WeakerAggregation is Weaker

    Furniture is not an intrinsic part of roomFurniture is not an intrinsic part of room

    Furniture can be shifted to another room, andFurniture can be shifted to another room, andso can exist independent of a particular roomso can exist independent of a particular room

  • 8/3/2019 Computer Notes - Multiple Inheritance

    40/40

    ExampleExampleAggregation is WeakerAggregation is Weaker

    A plant is not an intrinsic part of a gardenA plant is not an intrinsic part of a garden

    It can be planted in some other garden, and soIt can be planted in some other garden, and socan exist independent of a particular gardencan exist independent of a particular garden