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

An overview of object-oriented programming (oop) concepts, focusing on inheritance, generalization, sub-typing, specialization, and overriding. It explains how classes can be related through inheritance, the benefits of inheritance, and the concepts of generalization, sub-typing, specialization, and overriding. Real-life examples are given to illustrate these concepts.

Typology: Slides

2011/2012

Uploaded on 08/08/2012

anchita
anchita 🇮🇳

4.4

(7)

113 documents

1 / 26

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Object-Oriented Programming
(OOP)
Lecture No. 4
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a

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