Upload
sameer-hane
View
231
Download
1
Embed Size (px)
Citation preview
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 04
1/26
Object-OrientedObject-Oriented
Programming (OOP)Programming (OOP)
Lecture No. 4Lecture No. 4
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 04
2/26
Recap InheritanceRecap Inheritance
Derived ca!! inherit! a theDerived ca!! inherit! a the
characteri!tic! o" the ba!e ca!!characteri!tic! o" the ba!e ca!!
#e!ide! inherited characteri!tic!$#e!ide! inherited characteri!tic!$
derived ca!! ma% have it! o&n uni'uederived ca!! ma% have it! o&n uni'ue
characteri!tic!characteri!tic!
ajor bene"it o" inheritance i! reu!eajor bene"it o" inheritance i! reu!e
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 04
3/26
oncept! Reated &ithoncept! Reated &ith
InheritanceInheritance
*enerai+ation*enerai+ation
,ubt%ping (eten!ion),ubt%ping (eten!ion)
,peciai+ation (re!triction),peciai+ation (re!triction)
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 04
4/26
*enerai+ation*enerai+ation
In OO mode!$ !ome ca!!e! ma% haveIn OO mode!$ !ome ca!!e! ma% have
common characteri!tic!common characteri!tic!
e etract the!e "eature! into a ne&e etract the!e "eature! into a ne&
ca!! and inherit origina ca!!e! "romca!! and inherit origina ca!!e! "rom
thi! ne& ca!!thi! ne& ca!!
/hi! concept i! 0no&n a!/hi! concept i! 0no&n a!
*enerai+ation*enerai+ation
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 04
5/26
1ampe *enerai+ation1ampe *enerai+ation
Circle
colorvertices
radius
move
setColor
computeArea
Linecolor
vertices
lengthmove
setColor
getLength
Triangle
color
vertices
anglemove
setColor
computeArea
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 04
6/26
1ampe *enerai+ation1ampe *enerai+ation
Shapecolor
vertices
move
setColor
Circleradius
computeArea
Linelength
getLength
Triangleangle
computeArea
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 04
7/26
1ampe *enerai+ation1ampe *enerai+ation
Teachername
age
gender
designation
salaryteach
takeExameat
walk
Studentname
age
gender
program
studyYearstudy
heldExam
eat
walk
Doctor
nameage
gender
designation
salarycheckp
prescri!e
eat
walk
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 04
8/26
1ampe *enerai+ation1ampe *enerai+ation
Personnameage
gendereat
walk
Teacherdesignation
salary
teach
takeExam
Studentprogram
studyYear
study
heldExam
Doctordesignation
salary
checkp
prescri!e
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 04
9/26
,ub-t%ping 2 ,peciai+ation,ub-t%ping 2 ,peciai+ation
e &ant to add a ne& ca!! to ane &ant to add a ne& ca!! to anei!ting modeei!ting mode
3ind an ei!ting ca!! that aread%3ind an ei!ting ca!! that aread%impement! !ome o" the de!ired !tateimpement! !ome o" the de!ired !tateand behaviourand behaviour
Inherit the ne& ca!! "rom thi! ca!!Inherit the ne& ca!! "rom thi! ca!!and add uni'ue behaviour to the ne&and add uni'ue behaviour to the ne&
ca!!ca!!
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 04
10/26
,ub-t%ping (1ten!ion),ub-t%ping (1ten!ion)
,ub-t%ping mean! that derived ca!! i!,ub-t%ping mean! that derived ca!! i!
behavioura% compatibe &ith thebehavioura% compatibe &ith the
ba!e ca!!ba!e ca!!
#ehavioura% compatibe mean! that#ehavioura% compatibe mean! that
ba!e ca!! can be repaced b% theba!e ca!! can be repaced b% the
derived ca!!derived ca!!
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 04
11/26
1ampe 1ampe
,ub-t%ping,ub-t%ping
(1ten!ion)(1ten!ion)
Personname
agegender
eats
walks
Student
programstudyYear
study
takeExam
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 04
12/26
1ampe 1ampe
,ub-t%ping,ub-t%ping
(1ten!ion)(1ten!ion)
Shapecolor
vertices
setColor
move
Circleradius
computeC"
computeArea
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 04
13/26
,peciai+ation (Re!triction),peciai+ation (Re!triction)
,peciai+ation mean! that derived,peciai+ation mean! that derived
ca!! i! behavioura% incompatibeca!! i! behavioura% incompatibe
&ith the ba!e ca!!&ith the ba!e ca!!
#ehavioura% incompatibe mean! that#ehavioura% incompatibe mean! that
ba!e ca!! cant a&a%! be repaced b%ba!e ca!! cant a&a%! be repaced b%
the derived ca!!the derived ca!!
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 04
14/26
1ampe ,peciai+ation1ampe ,peciai+ation
(Re!triction)(Re!triction)
Personage # $%&&'%%(
)
Adultage # $'*&&'%%(
)
setAge+ a ,
)
setAge+ a ,
)
age - a
./ age 0 '* then error
else
age - a
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 04
15/26
1ampe ,peciai+ation1ampe ,peciai+ation
(Re!triction)(Re!triction)
IntegerSet)
1aturalSet)
add+ elem ,
)
add+ elem ,
)
add element
to the set
./ elem 0 ' then
errorelse
add element
to the set
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 04
16/26
OverridingOverriding
5 ca!! ma% need to override the5 ca!! ma% need to override thede"aut behaviour provided b% it! ba!ede"aut behaviour provided b% it! ba!e
ca!!ca!!
Rea!on! "or overridingRea!on! "or overriding
Provide behaviour !peci"ic to a derivedProvide behaviour !peci"ic to a derivedca!!ca!!
1tend the de"aut behaviour1tend the de"aut behaviour
Re!trict the de"aut behaviourRe!trict the de"aut behaviour
Im rove er"ormanceImprove per"ormance
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 04
17/26
1ampe ,peci"ic #ehaviour1ampe ,peci"ic #ehaviour
Shapecolor
vertices
draw
movesetColor
Circleradius
draw
computeArea
Linelength
draw
Triangle
angle
draw
computeArea
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 04
18/26
1ampe 1ten!ion1ampe 1ten!ion
2indowwidth
height
open
closedraw
Dialog3oxcontrols
ena!le
draw
'4 .nvoke 2indow5s
draw
64 draw the dialog
!ox
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 04
19/26
1ampe Re!triction1ampe Re!triction
IntegerSet)
1aturalSet)
add+ elem ,
)
add+ elem ,
)
Add element
to the set
./ elem 0 ' then
give errorelse
Add element
to the set
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 04
20/26
1ampe Improve Per"ormance1ampe Improve Per"ormance
a!! ircea!! irce
override!override! rotaterotate
operation o" ca!!operation o" ca!!,hape &ith a Nu,hape &ith a Nu
operation.operation.
Shapecolor
coord
draw
rotatesetColor
Circleradius
draw
rotate
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 04
21/26
5b!tract a!!e!5b!tract a!!e!
5n ab!tract ca!! impement! an5n ab!tract ca!! impement! an
ab!tract conceptab!tract concept
ain purpo!e i! to be inherited b%ain purpo!e i! to be inherited b%
other ca!!e!other ca!!e!
ant be in!tantiatedant be in!tantiatedPromote! reu!ePromote! reu!e
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 04
22/26
1ampe 5b!tract a!!e!1ampe 5b!tract a!!e!
TeacherDoctorStudent
6ere$ Per!on i! an ab!tract ca!!6ere$ Per!on i! an ab!tract ca!!
Personname
age
gender
eat
walk
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 04
23/26
1ampe 5b!tract a!!e!1ampe 5b!tract a!!e!
3usTruckCar
6ere$ 7ehice i! an ab!tract ca!!6ere$ 7ehice i! an ab!tract ca!!
Vehiclecolor
model
accelerateapply3rakes
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 04
24/26
oncrete a!!e!oncrete a!!e!
5 concrete ca!! impement! a5 concrete ca!! impement! a
concrete conceptconcrete concept
ain purpo!e i! to be in!tantiatedain purpo!e i! to be in!tantiated
Provide! impementation detai!Provide! impementation detai!!peci"ic to the domain contet!peci"ic to the domain contet
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 04
25/26
1ampe oncrete a!!e!1ampe oncrete a!!e!
6ere$ ,tudent$ /eacher and Doctor6ere$ ,tudent$ /eacher and Doctor
are concrete ca!!e!are concrete ca!!e!
TeacherDoctorStudent
program
studyYear
study
heldExam
7erson
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 04
26/26
1ampe oncrete a!!e!1ampe oncrete a!!e!
8 9ere: Car: 3us and Truck are concrete
classes
3usCar
Vehicle
Truckcapacity
load
unload