0% found this document useful (0 votes)
5 views

OOP-Lecture 04

C++

Uploaded by

shahwaizarts
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

OOP-Lecture 04

C++

Uploaded by

shahwaizarts
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 25

Object Oriented

Programming
Dr. Mujeeb Ur Rehman
[email protected]

Recommended Books:
1.C++ How to Program ( Deitel & Deitel );
2.Object-Oriented Software Engineering By Jacobson, Christerson, Jonsson,
Overgaard
Hazrat/Sir Allama Muhammad Iqbal (R.A)
Object-Oriented
Programming (OOP)
Lecture No. 4
Concepts Related with
Inheritance

► Generalization

► Subtyping (extension)

► Specialization (restriction)
Generalization
► In
OO models, some classes may have
common characteristics

► We extract these features into a new


class and inherit original classes from
this new class

► This concept is known as Generalization


Example – Generalization
Line
color
vertices Circle
length color
move vertices Triangle
setColor radius
color
getLength move
vertices
setColor
angle
computeArea
move
setColor
computeArea
Example – Generalization
Shape
color
vertices
move
setColor

Circle Triangle
radius Line angle
computeArea length computeArea
getLength
Example – Generalization
Student
name
age Teacher
gender name
Doctor
program age
name
studyYear gender
age
study designation
gender
heldExam salary
teach designation
eat
takeExam salary
walk
eat checkUp
walk prescribe
eat
walk
Example – Generalization
Person
name
age
gender
eat
walk

Student Teacher Doctor


program designation designation
studyYear salary salary
study teach checkUp
heldExam takeExam prescribe
Sub-typing & Specialization
► We want to add a new class to an
existing model

► Find
an existing class that already
implements some of the desired state
and behaviour

► Inherit
the new class from this class and
add unique behaviour to the new class
Sub-typing (Extension)
► Sub-typingmeans that derived class is
behaviourally compatible with the
base class

► Behaviourallycompatible means that


base class can be replaced by the
derived class
Person
name
age
gender
eats
Example – walks

Sub-typing
(Extension) Student
program
studyYear
study
takeExam
Shape
color
vertices
setColor
Example – move

Sub-typing
(Extension)
Circle
radius
computeCF
computeArea
Specialization (Restriction)
► Specializationmeans that derived
class is behaviourally incompatible
with the base class

► Behaviourallyincompatible means that


base class can’t always be replaced by
the derived class
Example – Specialization
(Restriction)
Person
age : [0..100]

setAge( a ) age = a

Adult If age < 18 then


age : [18..100] error
… else
setAge( a ) age = a

Example – Specialization
(Restriction)
IntegerSet

add( elem ) add element
… to the set

If elem < 1 then


NaturalSet error
… else
add( elem ) add element
… to the set
Overriding

►A class may need to override the default


behaviour 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
color
vertices
draw
move
setColor

Circle Triangle
radius Line angle
draw length draw
computeArea draw computeArea
Abstract Classes

► An abstract class implements an


abstract concept
► Main purpose is to be inherited by
other classes
► Can’t be instantiated
► Promotes reuse
Example – Abstract Classes
Person
name
age
gender
eat
walk

Student Doctor
Teacher
► Here, Person is an abstract class
Example – Abstract Classes
Vehicle
color
model
accelerate
applyBrakes

Car Truck
Bus

► Here, Vehicle is an abstract class


Concrete Classes
►A concrete class implements a
concrete concept

► Main purpose is to be instantiated

► Provides implementation details


specific to the domain context
Example – Concrete Classes

Person

Student Doctor
program Teacher
studyYear
study
heldExam

► Here,Student, Teacher and Doctor


are concrete classes
Example – Concrete Classes

Vehicle

Car Truck
Bus
capacity
load
unload

• Here, Car, Bus and Truck are concrete


classes

You might also like