8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 06
1/43
Object-Oriented Programming(OOP)
Lecture No. 6
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 06
2/43
Class Compatibility
A class is behaviorally compatible withanother if it supports all the operations ofthe other class
Such a class is called subtype
A class can be replaced by its subtype
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 06
3/43
Class Compatibility
Derived class is usually a subtype of thebase class
It can handle all the legal messages(operations) of the base class
Therefore, base class can always bereplaced by the derived class
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 06
4/43
ExampleClass Compatibility
Shapecolor
vertices
move
setColordraw
Circleradiusdraw
computeArea
Linelength
draw
getLength
Triangle
angle
draw
computeArea
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 06
5/43
ExampleClass Compatibility
Filesize
open
ASCII Fileprint
PDF File
PS File
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 06
6/43
Polymorphism
In general, polymorphism refers toexistence of different forms of a singleentity
For example, both Diamond and Coal aredifferent forms of Carbon
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 06
7/43
Polymorphism in OO Model
In OO model, polymorphism means thatdifferent objects can behave in differentways for the same message (stimulus)
Consequently, sender of a message does
not need to know exact class of the receiver
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 06
8/43
ExamplePolymorphism
Shape
Line Circle Triangle
draw
draw
draw draw
drawView
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 06
9/43
ExamplePolymorphism
File
ASCII File PDF File PS File
print print
printEditor
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 06
10/43
PolymorphismAdvantages
Messages can be interpreted in differentways depending upon the receiver class
Shape
Line Circle Triangledraw
draw
draw draw
drawView
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 06
11/43
PolymorphismAdvantages
New classes can be added without changingthe existing model
Squaredraw
Shape
Line Circle Triangledraw
draw
draw draw
drawView
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 06
12/43
PolymorphismAdvantages
In general, polymorphism is a powerful toolto develop flexible and reusable systems
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 06
13/43
Object-Oriented Modeling
An Example
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 06
14/43
Problem Statement
Develop a graphic editor that can drawdifferent geometric shapes such as line,circle and triangle. User can select, move or
rotate a shape. To do so, editor providesuser with a menu listing differentcommands. Individual shapes can be
grouped together and can behave as asingle shape.
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 06
15/43
Identify Classes
Extract nouns in the problem statement
Develop a graphic editorthat can drawdifferent geometric shapessuch as line,circleand triangle. Usercan select, move orrotate a shape. To do so, editorprovides
userwith a menulisting differentcommands. Individual shapescan begrouped together and can behave as asingle shape.
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 06
16/43
Identify Classes
Eliminate irrelevant classes
EditorVery broad scope
UserOut of system boundary
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 06
17/43
Identify Classes
Add classes by analyzing requirements
Grouprequired to behave as a shapeIndividual shapes can be grouped together and
can behave as a single shape
Vieweditor must have a display area
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 06
18/43
Identify Classes
Shape
Line
Circle
TriangleMenu
Group
View
Following classes have been identified:
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 06
19/43
Object ModelGraphic Editor
Line
Circle
Triangle
GroupShape
View
Menu
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 06
20/43
Identify Associations
Extract verbs connecting objects
Individual shapes can be groupedtogether
Group consists of lines, circles, triangles
Group can also consists of other groups
(Composition)
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 06
21/43
Identify Associations
Verify access paths
View contains shapesView contains lines
View contains circles
View contains triangles
View contains groups
(Aggregation)
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 06
22/43
Identify Associations
Verify access paths
Menu sends message to View(Simple One-Way Association)
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 06
23/43
Object ModelGraphic Editor
TriangleCircleLine
ShapeView
nnnnnn
nn
Menu
Group
nn
nnnn
nn
nn
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 06
24/43
Identify Attributes
Extract properties of the object
From the problem statement
Properties are not mentioned
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 06
25/43
Identify Attributes
Extract properties of the object From the domain knowledge
Line
Color
Vertices
Length
Circle Color
Vertices
Radius
Triangle
Color
Vertices
Angle
Shape Color
Vertices
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 06
26/43
Identify Attributes
Extract properties of the object From the domain knowledge
Group
noOfObjects
View
noOfObjects
selected
Menu
Name
isOpen
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 06
27/43
Object ModelGraphic Editor
Menu
nameisOpen
View
noOfObjectsselected
Shape
colorvertices
Linelength
Circleradius GroupnoOfObjectsTriangleangle
nn
n
nn
n
nn
n
n
nn
nn
nn
n
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 06
28/43
Identify Operations
Extract verbs connected with an object
Developa graphic editor that can draw
different geometric shapes such as line,circle and triangle. User can select, move
or rotatea shape. To do so, editor provides
user with a menu listing different
commands. Individual shapes can be
groupedtogether and can behaveas a
single shape.
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 06
29/43
Identify Operations
Eliminate irrelevant operations
Develop out of system boundary
Behavehave broad semantics
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 06
30/43
Identify Operations
Following are selected operations:
Line
Draw
Select
Move
Rotate
Circle
Draw
Select
Move
Rotate
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 06
31/43
Identify Operations
Following are selected operations:
Triangle
Draw
Select
Move
Rotate
Shape
Draw
Select
Move
Rotate
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 06
32/43
Identify Operations
Following are selected operations:
Group
Draw Select
Move
Rotate
Menu
Open Select
Move
Rotate
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 06
33/43
Identify Operations
Extract operations using domainknowledge
ViewAdd
Remove
Group
Show
Select
Move
Rotate
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 06
34/43
Group
noOfObjects
draw()
Triangle
angle
draw()nnCircle
radius
draw()nn
Line
length
draw()
nn
Shape
colorvertices
draw()select()move()
rotate()nn
View
noOfObjectsselected
add()remove()group()show()select()move()rotate()
nn
nnnn
nn
Menu
nameisOpen
open()select()move()
rotate()
n
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 06
35/43
Identify Inheritance
Search is a kind of by looking at keywordslike such as, for example, etc
shapes such as line, circle and triangle Line, Circle and Triangle inherits from Shape
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 06
36/43
Identify Inheritance
By analyzing requirements
Individual shapes can be grouped togetherand can behave as a single shape
Group inherits from Shape
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 06
37/43
Refining the Object Model
Application of inheritance demands aniteration over the whole object model
In the inheritance hierarchy,
All attributes are shared
All associations are shared
Some operations are shared
Others are overridden
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 06
38/43
Refining the Object Model
Share associations
View contains all kind of shapes
Group consists of all kind of shapes
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 06
39/43
Refining the Object Model
Share attributes
ShapeLine, Circle, Triangle and Group
Color, vertices
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 06
40/43
Refining the Object Model
Share operations
ShapeLine, Circle, Triangle and Group
Select
Move Rotate
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 06
41/43
Refining the Object Model
Share the interface and overrideimplementation
ShapeLine, Circle, Triangle and Group
Draw
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 06
42/43
Line
length
draw()
Circle
radius
draw()
Triangle
angle
draw()
Group
noOfObjects
draw()
Shape
colorvertices
draw()select()move()
rotate()
n
View
noOfObjects
selected
add()remove()group()show()
select()move()rotate()
nn
Menu
nameisOpen
open()select()move()
rotate()
n
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 06
43/43
Group
noOfObjects
draw()
Triangle
angle
draw()nnCircle
radius
draw()nn
Line
length
draw()
nn
Shape
colorvertices
draw()select()move()
rotate()nn
View
noOfObjectsselected
add()remove()group()show()select()move()rotate()
nn
nnnn
nn
Menu
nameisOpen
open()select()move()
rotate()
n