0% found this document useful (0 votes)
131 views26 pages

Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 04

This document discusses object-oriented programming concepts related to inheritance. It defines key concepts like generalization, which is creating a new class that inherits common characteristics from existing classes. Specialization and subtyping are discussed, where a new class inherits from an existing class and adds unique behavior. Overriding is covered, where a derived class can override default behavior from the base class. Abstract classes are explained as classes that can't be instantiated but are intended to be inherited from to promote reuse. Concrete classes provide implementations for abstract concepts. Examples are provided to illustrate these inheritance-related OOP concepts.

Uploaded by

Sameer Hane
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
Download as ppt, pdf, or txt
0% found this document useful (0 votes)
131 views26 pages

Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 04

This document discusses object-oriented programming concepts related to inheritance. It defines key concepts like generalization, which is creating a new class that inherits common characteristics from existing classes. Specialization and subtyping are discussed, where a new class inherits from an existing class and adds unique behavior. Overriding is covered, where a derived class can override default behavior from the base class. Abstract classes are explained as classes that can't be instantiated but are intended to be inherited from to promote reuse. Concrete classes provide implementations for abstract concepts. Examples are provided to illustrate these inheritance-related OOP concepts.

Uploaded by

Sameer Hane
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1/ 26

Object-Oriented Programming (OOP) Lecture No.

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

)oncept! Re ated &ith Inheritance


*enera i+ation ,ubt%ping

(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!

1-amp e *enera i+ation


Line
color vertices length move setColor getLength color vertices radius move setColor computeArea

Circle

Triangle
color vertices angle move setColor computeArea

1-amp e *enera i+ation


Shape
color vertices move setColor

Circle
radius computeArea

Triangle Line
length getLength angle computeArea

1-amp e *enera i+ation


Student
name age gender program studyYear study heldExam eat walk

Teacher
name age gender designation salary teach takeExam eat walk

Doctor
name age gender designation salary check p prescri!e eat walk

1-amp e *enera i+ation


name age gender eat walk

Person

Student
program studyYear study heldExam

Teacher
designation salary teach takeExam

Doctor
designation salary check p prescri!e

,ub-t%ping 2 ,pecia i+ation


.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

1-amp e ,ub-t%ping (1-ten!ion)

Student
program studyYear study takeExam

Shape
color vertices setColor move

1-amp e ,ub-t%ping (1-ten!ion)

Circle
radius computeC" computeArea

,pecia i+ation (Re!triction)


,pecia i+ation

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

1-amp e ,pecia i+ation (Re!triction)


Person
age # $%&&'%%( ) setAge+ a , )

age - a

Adult
age # $'*&&'%%( ) setAge+ a , )

./ age 0 '* then error else age - a

1-amp e ,pecia i+ation (Re!triction)


IntegerSet
) add+ elem , ) add element to the set

1aturalSet
) add+ elem , )

./ elem 0 ' then error else add element to the set

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

1-amp e ,peci"ic #ehaviour


Shape
color vertices draw move setColor

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

'4 .nvoke 2indow5s draw 64 draw the dialog !ox

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

1-amp e Improve Per"ormance


Shape
) a!!

)irc e override! rotate operation o" c a!! ,hape &ith a Nu operation.

color coord draw rotate setColor

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

1-amp e 5b!tract ) a!!e!


Person
name age gender eat walk

Student
6ere$

Teacher

Doctor

Per!on i! an ab!tract c a!!

1-amp e 5b!tract ) a!!e!


Vehicle
color model accelerate apply3rakes

Car
6ere$

Truck 3us

7ehic e i! an ab!tract c a!!

)oncrete ) a!!e!
5

concrete c a!! imp ement! a concrete concept purpo!e i! to be in!tantiated

(ain

Provide!

imp ementation detai ! !peci"ic to the domain conte-t

1-amp e )oncrete ) a!!e!


7erson
Student
program studyYear study heldExam

Teacher

Doctor

6ere$

,tudent$ /eacher and Doctor are concrete c a!!e!

1-amp e )oncrete ) a!!e!


Vehicle Car Truck
capacity load unload

3us

8 9ere: Car: 3us and Truck are concrete classes

You might also like