Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 04
Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 04
Recap Inheritance
Derived
c a!! inherit! a the characteri!tic! o" the ba!e c a!! inherited characteri!tic!$ derived c a!! ma% have it! o&n uni'ue characteri!tic! bene"it o" inheritance i! reu!e
#e!ide!
(ajor
(e-ten!ion) (re!triction)
,pecia i+ation
*enera i+ation
In
OO mode !$ !ome c a!!e! ma% have common characteri!tic! e-tract the!e "eature! into a ne& c a!! and inherit origina c a!!e! "rom thi! ne& c a!! concept i! 0no&n a! *enera i+ation
.e
/hi!
Circle
Triangle
color vertices angle move setColor computeArea
Circle
radius computeArea
Triangle Line
length getLength angle computeArea
Teacher
name age gender designation salary teach takeExam eat walk
Doctor
name age gender designation salary check p prescri!e eat walk
Person
Student
program studyYear study heldExam
Teacher
designation salary teach takeExam
Doctor
designation salary check p prescri!e
&ant to add a ne& c a!! to an e-i!ting mode an e-i!ting c a!! that a read% imp ement! !ome o" the de!ired !tate and behaviour the ne& c a!! "rom thi! c a!! and add uni'ue behaviour to the ne& c a!!
3ind
Inherit
,ub-t%ping (1-ten!ion)
,ub-t%ping
mean! that derived c a!! i! behavioura % compatib e &ith the ba!e c a!! % compatib e mean! that ba!e c a!! can be rep aced b% the derived c a!!
#ehavioura
Person
name age gender eats walks
Student
program studyYear study takeExam
Shape
color vertices setColor move
Circle
radius computeC" computeArea
mean! that derived c a!! i! behavioura % incompatib e &ith the ba!e c a!! % incompatib e mean! that ba!e c a!! can4t a &a%! be rep aced b% the derived c a!!
#ehavioura
age - a
Adult
age # $'*&&'%%( ) setAge+ a , )
1aturalSet
) add+ elem , )
Overriding
5
c a!! ma% need to override the de"au t behaviour provided b% it! ba!e c a!! "or overriding
Rea!on!
Provide behaviour !peci"ic to a derived c a!! 1-tend the de"au t behaviour Re!trict the de"au t behaviour Improve per"ormance
Circle
radius draw computeArea
Triangle Line
length draw angle draw computeArea
1-amp e 1-ten!ion
2indow
width height open close draw
Dialog3ox
controls ena!le draw
1-amp e Re!triction
IntegerSet
) add+ elem , ) Add element to the set
1aturalSet
) add+ elem , )
./ elem 0 ' then give error else Add element to the set
Circle
radius draw rotate
5b!tract ) a!!e!
5n
ab!tract c a!! imp ement! an ab!tract concept (ain purpo!e i! to be inherited b% other c a!!e! )an4t be in!tantiated Promote! reu!e
Student
6ere$
Teacher
Doctor
Car
6ere$
Truck 3us
)oncrete ) a!!e!
5
(ain
Provide!
Teacher
Doctor
6ere$
3us