Docsity

OOP Lecture 4: Inheritance, Generalization, Sub-typing, Specialization, Overriding, Slides of Object Oriented Programming

Partial preview of the text

Download OOP Lecture 4: Inheritance, Generalization, Sub-typing, Specialization, Overriding and more Slides Object Oriented Programming in PDF only on Docsity!

Object-Oriented Programming

(OOP)

Lecture No. 4

Recap – Inheritance

Derived class inherits all the characteristicsof the base class

Besides inherited characteristics, derivedclass may have its own uniquecharacteristics

Major benefit of inheritance is reuse

Generalization

In OO models, some classes may havecommon characteristics

We extract these features into a new classand inherit original classes from this newclass

This concept is known as Generalization

Example – Generalization

Circle

colorverticesradiusmovesetColorcomputeArea

Line

colorverticeslengthmovesetColorgetLength

Triangle

colorverticesanglemovesetColorcomputeArea

Example – Generalization

Teacher

nameagegenderdesignationsalaryteachtakeExameatwalk

Student

nameagegenderprogramstudyYearstudyheldExameatwalk

Doctor

nameagegenderdesignationsalarycheckUpprescribeeat

Example – Generalization

Person

nameagegendereatwalk

Teacher

designationsalaryteach

Student

programstudyYearstudy

Doctor

designationsalarycheckUp

Sub-typing (Extension)

Sub-typing means that derived class isbehaviourally compatible with the base class

Behaviourally compatible means that baseclass can be replaced by the derived class

Example –

Sub-typing

(Extension)

Person

nameagegendereatswalks

Student

programstudyYearstudy

Specialization (Restriction)

Specialization means that derived class isbehaviourally incompatible with the baseclass

Behaviourally incompatible means that baseclass can’t always be replaced by thederived class

Example – Specialization

(Restriction)

Person

age : [0..100]…

Adult

age : [18..100]…

setAge( a )… setAge( a )

age = a

If age < 18 then

error

else

age = a

Overriding

A class may need to override the defaultbehaviour provided by its base class

Reasons for overriding

Provide behaviour specific to a derived class

Extend the default behaviour

Restrict the default behaviour

Improve performance

Example – Specific Behaviour

Shape

colorverticesdrawmovesetColor

Circle

radiusdraw

Line

length

Triangle

angledraw

Example – Restriction

IntegerSet

NaturalSet

add( elem )… add( elem )…

Add elementto the set If elem < 1 then

give error

else

Add elementto the set

Example – Improve Performance

►

Class Circle overridesrotate operation ofclass Shape with a Nulloperation.

Shape

colorcoorddrawrotatesetColor

Circle

radiusdraw