computer notes - polymorphism

Upload: ecomputernotes

Post on 06-Apr-2018

224 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/3/2019 Computer Notes - Polymorphism

    1/43

    PolymorphismPolymorphism

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Polymorphism

    2/43

    Class CompatibilityClass Compatibility

    A class is behaviorally compatible with anotherA class is behaviorally compatible with another

    if it supports all the operations of the other classif it supports all the operations of the other class

    Such a class is called subtypeSuch a class is called subtype

    A class can be replaced by its subtypeA class can be replaced by its subtype

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Polymorphism

    3/43

    Class CompatibilityClass Compatibility

    Derived class is usually a subtype of the baseDerived class is usually a subtype of the base

    classclass

    It can handle all the legal messagesIt can handle all the legal messages(operations) of the base class(operations) of the base class

    Therefore, base class can always be replaced byTherefore, base class can always be replaced by

    the derived classthe derived class

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Polymorphism

    4/43

    ExampleExample Class CompatibilityClass CompatibilityShape

    colorvertices

    move

    setColor

    draw

    Circleradiusdraw

    computeArea

    Linelength

    draw

    getLength

    Triangle

    angledraw

    computeArea

  • 8/3/2019 Computer Notes - Polymorphism

    5/43

    ExampleExample Class CompatibilityClass CompatibilityFile

    size

    open

    print

    ASCII Fileprint

    PDF File

    print

    PS File

    print

  • 8/3/2019 Computer Notes - Polymorphism

    6/43

    PolymorphismPolymorphism

    In general, polymorphism refers to existence ofIn general, polymorphism refers to existence of

    different forms of a single entitydifferent forms of a single entity

    For example, both Diamond and Coal areFor example, both Diamond and Coal are

    different forms of Carbondifferent forms of Carbon

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Polymorphism

    7/43

    Polymorphism in OO ModelPolymorphism in OO Model

    In OO model, polymorphism means thatIn OO model, polymorphism means that

    different objects can behave in different waysdifferent objects can behave in different ways

    for the same message (stimulus)for the same message (stimulus)

    Consequently, sender of a message does notConsequently, sender of a message does not

    need to know exact class of the receiverneed to know exact class of the receiver

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Polymorphism

    8/43

    ExampleExample PolymorphismPolymorphism

    Shape

    Line Circle Triangle

    draw

    draw

    draw draw

    drawView

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Polymorphism

    9/43

    ExampleExample PolymorphismPolymorphism

    File

    ASCII File PDF File PS File

    print

    print

    print print

    printEditor

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Polymorphism

    10/43

    PolymorphismPolymorphismAdvantagesAdvantages

    Messages can be interpreted in different waysMessages can be interpreted in different ways

    depending upon the receiver classdepending upon the receiver class

    Shape

    Line Circle Triangledraw

    draw

    draw draw

    drawView

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Polymorphism

    11/43

    PolymorphismPolymorphismAdvantagesAdvantages

    New classes can be added without changing theNew classes can be added without changing the

    existing modelexisting model

    Squaredraw

    Shape

    Line Circle Triangledraw

    draw

    draw draw

    drawView

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Polymorphism

    12/43

    PolymorphismPolymorphismAdvantagesAdvantages

    In general, polymorphism is a powerful tool toIn general, polymorphism is a powerful tool to

    develop flexible and reusable systemsdevelop flexible and reusable systems

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Polymorphism

    13/43

    ObjectObject--Oriented ModelingOriented Modeling

    An ExampleAn Example

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Polymorphism

    14/43

    Problem StatementProblem Statement

    Develop a graphic editor that can draw differentDevelop a graphic editor that can draw different

    geometric shapes such as line, circle andgeometric shapes such as line, circle andtriangle. User can select, move or rotate atriangle. User can select, move or rotate a

    shape. To do so, editor provides user with ashape. To do so, editor provides user with a

    menu listing different commands. Individualmenu listing different commands. Individual

    shapes can be grouped together and canshapes can be grouped together and can

    behave as a single shape.behave as a single shape.

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Polymorphism

    15/43

    Identify ClassesIdentify Classes Extract nouns in the problem statementExtract nouns in the problem statement

    Develop a graphicDevelop a graphic editoreditor that can draw differentthat can draw differentgeometricgeometric shapesshapes such assuch as lineline,, circlecircle andandtriangletriangle.. UserUser can select, move or rotate acan select, move or rotate ashapeshape. To do so,. To do so, editoreditor providesprovides useruser with awith amenumenu listing differentlisting different commandscommands. Individual. Individual

    shapesshapes can be grouped together and cancan be grouped together and canbehave as a singlebehave as a single shapeshape..

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Polymorphism

    16/43

    Identify ClassesIdentify Classes Eliminate irrelevant classesEliminate irrelevant classes

    EditorEditorVery broad scopeVery broad scope

    UserUser Out of system boundaryOut of system boundary

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Polymorphism

    17/43

    Identify ClassesIdentify ClassesAdd classes by analyzing requirementsAdd classes by analyzing requirements

    GroupGroup required to behave as a shaperequired to behave as a shape

    Individual shapes can be grouped together and canIndividual shapes can be grouped together and canbehave as a single shapebehave as a single shape

    ViewView editor must have a display areaeditor must have a display area

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Polymorphism

    18/43

    Identify ClassesIdentify Classes

    ShapeShape

    LineLineCircleCircle

    TriangleTriangle

    MenuMenu

    Group

    View

    Following classes have been identified:

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Polymorphism

    19/43

    Object ModelObject Model Graphic EditorGraphic Editor

    Line

    Circle

    Triangle

    GroupShape

    View

    Menu

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Polymorphism

    20/43

    Identify AssociationsIdentify Associations Extract verbs connecting objectsExtract verbs connecting objects

    Individual shapes can be grouped togetherIndividual shapes can be grouped together

    Group consists of lines, circles, trianglesGroup consists of lines, circles, triangles Group can also consists of other groupsGroup can also consists of other groups

    (Composition)(Composition)

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Polymorphism

    21/43

    Identify AssociationsIdentify AssociationsVerify access pathsVerify access paths

    View contains shapesView contains shapes

    View contains linesView contains linesView contains circlesView contains circles

    View contains trianglesView contains triangles

    View contains groupsView contains groups

    (Aggregation)(Aggregation)

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Polymorphism

    22/43

    Identify AssociationsIdentify AssociationsVerify access pathsVerify access paths

    Menu sends message to ViewMenu sends message to View

    (Simple One(Simple One--Way Association)Way Association)

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Polymorphism

    23/43

    Object ModelObject Model Graphic EditorGraphic Editor

    TriangleCircleLine

    ShapeView

    nnnnnn

    nnMenu

    Group

    nn

    nn

    nn

    nn

    nn

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Polymorphism

    24/43

    Identify AttributesIdentify Attributes Extract properties of the objectExtract properties of the object

    From the problem statementFrom the problem statement

    Properties are not mentionedProperties are not mentioned

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Polymorphism

    25/43

    Identify AttributesIdentify Attributes Extract properties of the objectExtract properties of the object

    From the domain knowledgeFrom the domain knowledge

    Line Color

    Vertices

    Length

    Circle Color Vertices

    Radius

    Triangle Color

    Vertices

    Angle

    Shape

    Color

    Vertices

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Polymorphism

    26/43

    Identify AttributesIdentify Attributes Extract properties of the objectExtract properties of the object

    From the domain knowledgeFrom the domain knowledge

    Group noOfObjects

    View noOfObjects

    selected

    Menu Name

    isOpen

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Polymorphism

    27/43

    Object ModelObject Model Graphic EditorGraphic Editor

    Menu

    nameisOpen

    View

    noOfObjectsselected

    Shape

    colorvertices

    Line

    length

    Circle

    radius

    Group

    noOfObjects

    Triangle

    angle

    nn

    n

    nn

    n

    nn

    n

    n

    nn

    nn

    nn

    n

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Polymorphism

    28/43

    Identify OperationsIdentify Operations Extract verbs connected with an objectExtract verbs connected with an object

    Develop a graphic editor that can draw

    different geometric shapes such as line, circle

    and triangle. User can select, move orrotate ashape. To do so, editorprovides user with a

    menu listing different commands. Individual

    shapes can be grouped together and canbehave as a single shape.

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Polymorphism

    29/43

    Identify OperationsIdentify Operations Eliminate irrelevant operationsEliminate irrelevant operations

    DevelopDevelop out of system boundaryout of system boundary

    BehaveBehave have broad semanticshave broad semantics

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Polymorphism

    30/43

    Identify OperationsIdentify Operations Following are selected operations:Following are selected operations:

    Line Draw

    Select

    Move

    Rotate

    Circle Draw

    Select

    Move

    Rotate

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Polymorphism

    31/43

    Identify OperationsIdentify Operations Following are selected operations:Following are selected operations:

    Triangle Draw

    Select

    Move

    Rotate

    Shape Draw

    Select

    Move

    Rotate

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Polymorphism

    32/43

    Identify OperationsIdentify Operations Following are selected operations:Following are selected operations:

    Group

    Draw

    Select Move

    Rotate

    Menu

    Open

    Select Move

    Rotate

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Polymorphism

    33/43

    Identify OperationsIdentify Operations Extract operations using domain knowledgeExtract operations using domain knowledge

    View

    Add Remove

    Group

    Show

    Select

    Move

    Rotate

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Polymorphism

    34/43

    Group

    noOfObjects

    draw()

    Triangle

    angle

    draw() nn

    Circle

    radius

    draw()nn

    Line

    length

    draw()

    nn

    Shape

    colorvertices

    draw()select()move()rotate()

    nn

    ViewnoOfObjectsselected

    add()

    remove()group()show()select()move()

    rotate()nn

    nnnn

    nn

    Menu

    nameisOpen

    open()select()move()rotate()

    n

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Polymorphism

    35/43

    Identify InheritanceIdentify Inheritance

    SearchSearchis a kind ofis a kind ofby looking at keywords likeby looking at keywords likesuch assuch as,,for examplefor example, etc, etc

    shapes such as line, circle and triangleshapes such as line, circle and triangle Line, Circle and Triangle inherits from ShapeLine, Circle and Triangle inherits from Shape

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Polymorphism

    36/43

    Identify InheritanceIdentify Inheritance

    By analyzing requirementsBy analyzing requirements

    Individual shapes can be grouped together andIndividual shapes can be grouped together andcan behave as a single shapecan behave as a single shape

    Group inherits from ShapeGroup inherits from Shape

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Polymorphism

    37/43

    Refining the Object ModelRefining the Object ModelApplication of inheritance demands an iterationApplication of inheritance demands an iteration

    over the whole object modelover the whole object model

    In the inheritance hierarchy,In the inheritance hierarchy,All attributes are sharedAll attributes are shared

    All associations are sharedAll associations are shared

    Some operations are sharedSome operations are shared Others are overriddenOthers are overridden

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Polymorphism

    38/43

    Refining the Object ModelRefining the Object Model

    Share associationsShare associations

    View contains all kind of shapesView contains all kind of shapes

    Group consists of all kind of shapesGroup consists of all kind of shapes

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Polymorphism

    39/43

    Refining the Object ModelRefining the Object Model

    Share attributesShare attributes

    ShapeShape Line, Circle, Triangle and GroupLine, Circle, Triangle and Group Color, verticesColor, vertices

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Polymorphism

    40/43

    Refining the Object ModelRefining the Object Model

    Share operationsShare operations

    ShapeShape Line, Circle, Triangle and GroupLine, Circle, Triangle and Group SelectSelect

    MoveMove

    RotateRotate

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Polymorphism

    41/43

    Refining the Object ModelRefining the Object Model

    Share the interface and override implementationShare the interface and override implementation

    ShapeShape Line, Circle, Triangle and GroupLine, Circle, Triangle and Group DrawDraw

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Polymorphism

    42/43

    Linelength

    draw()

    Circleradius

    draw()

    Triangleangle

    draw()

    GroupnoOfObjects

    draw()

    Shape

    colorvertices

    draw()select()move()rotate()

    n

    ViewnoOfObjectsselected

    add()

    remove()group()show()select()move()

    rotate()

    nn

    Menu

    nameisOpen

    open()select()move()rotate()

    n

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Polymorphism

    43/43

    Group

    noOfObjects

    draw()

    Triangle

    angle

    draw() nn

    Circle

    radius

    draw()nn

    Line

    length

    draw()

    nn

    Shape

    colorvertices

    draw()select()move()rotate()

    nn

    ViewnoOfObjectsselected

    add()

    remove()group()show()select()move()

    rotate()nn

    nnnn

    nn

    Menu

    nameisOpen

    open()select()move()rotate()

    n

    http://ecomputernotes.com