CSC241: Object Oriented Programming: Fall 2016
CSC241: Object Oriented Programming: Fall 2016
CSC241:
Object Oriented
April 4, 2019
Information Technology
COMSATS Institute of
2
Recap – Inheritance
• Derived class inherits all the characteristics of the base class
April 4, 2019
• Besides inherited characteristics, derived class may have its
own unique characteristics
Information Technology
COMSATS Institute of
• Major benefit of inheritance is reuse
3
Concepts Related with
Inheritance
• Generalization
April 4, 2019
• Subtyping (extension)
Information Technology
COMSATS Institute of
• Specialization (restriction)
4
Generalization
• In OO models, some classes may have common characteristics
April 4, 2019
• We extract these features into a new class and inherit original
classes from this new class
Information Technology
COMSATS Institute of
• This concept is known as Generalization
5
Example – Generalization
Line
April 4, 2019
color
vertices Circle
length color
Information Technology
COMSATS Institute of
move vertices Triangle
setColor radius
color
getLength move
vertices
setColor
angle
computeArea
move
setColor
6
computeArea
Example – Generalization
Shape
color
April 4, 2019
vertices
move
setColor
Information Technology
COMSATS Institute of
Circle Triangle
radius Line angle
computeArea length computeArea 7
getLength
Example – Generalization
Student
name
April 4, 2019
age Teacher
gender name
Doctor
program age
name
Information Technology
COMSATS Institute of
studyYear gender
age
study designation
gender
heldExam salary
designation
eat teach
salary
walk takeExam
eat checkUp
walk prescribe
eat 8
walk
Example – Generalization
Person
name
April 4, 2019
age
gender
eat
Information Technology
COMSATS Institute of
walk
April 4, 2019
• Find an existing class that already implements some of the
desired state and behaviour
Information Technology
COMSATS Institute of
• Inherit the new class from this class and add unique behaviour
to the new class
10
Sub-typing (Extension)
• Sub-typing means that derived class is behaviourally
compatible with the base class
April 4, 2019
• Behaviourally compatible means that base class can be
Information Technology
COMSATS Institute of
replaced by the derived class
11
Person
name
age
gender
April 4, 2019
eats
Example – walks
Sub-typing
Information Technology
COMSATS Institute of
(Extension) Student
program
studyYear
study 12
takeExam
Shape
color
vertices
April 4, 2019
setColor
Example – move
Sub-typing
Information Technology
COMSATS Institute of
(Extension)
Circle
radius
computeCF
computeArea 13
Specialization (Restriction)
• Specialization means that derived class is behaviourally
incompatible with the base class
April 4, 2019
• Behaviourally incompatible means that base class can’t always
Information Technology
COMSATS Institute of
be replaced by the derived class
14
Example – Specialization
(Restriction)
Person
April 4, 2019
age : [0..100]
…
setAge( a ) age = a
Information Technology
COMSATS Institute of
…
April 4, 2019
…
add( elem ) add element
… to the set
Information Technology
COMSATS Institute of
If elem < 1 then
NaturalSet error
… else
add( elem ) add element
… to the set 16
Overriding
April 4, 2019
by its base class
Information Technology
COMSATS Institute of
• Provide behaviour specific to a derived class
• Extend the default behaviour
• Restrict the default behaviour
• Improve performance
17
Example – Specific Behaviour
Shape
color
April 4, 2019
vertices
draw
move
Information Technology
COMSATS Institute of
setColor
Circle Triangle
radius Line angle
18
draw length draw
computeArea draw computeArea
Example – Extension
Window
width
April 4, 2019
height
open
close
Information Technology
COMSATS Institute of
draw
April 4, 2019
add( elem ) Add element
… to the set
Information Technology
COMSATS Institute of
If elem < 1 then
NaturalSet give error
… else
add( elem ) Add element
… to the set 20
Example – Improve Performance
Shape
color
• Class Circle overrides coord
rotate operation of class draw
Shape with a Null rotate
operation. setColor
Circle
radius
draw
rotate
COMSATS Institute of Information
April 4, 2019
Technology 21
Abstract Classes
April 4, 2019
An abstract class implements an abstract concept
• Main purpose is to be inherited by other classes
• Can’t be instantiated
Information Technology
COMSATS Institute of
• Promotes reuse
22
Example – Abstract Classes
Person
name
April 4, 2019
age
gender
Information Technology
COMSATS Institute of
eat
walk
Student Doctor
Teacher
23
• Here, Person is an abstract class
Example – Abstract Classes
Vehicle
April 4, 2019
color
model
accelerate
Information Technology
COMSATS Institute of
applyBrakes
Car Truck
Bus
24
• Here, Vehicle is an abstract class
Concrete Classes
• A concrete class implements a concrete concept
April 4, 2019
• Main purpose is to be instantiated
Information Technology
COMSATS Institute of
• Provides implementation details specific to the domain
context
25
Example – Concrete Classes
Person
April 4, 2019
Information Technology
COMSATS Institute of
Student Doctor
program Teacher
studyYear
study
heldExam
26
• Here, Student, Teacher and Doctor are concrete classes
Example – Concrete Classes
Vehicle
April 4, 2019
Information Technology
COMSATS Institute of
Car Truck
Bus
capacity
load
unload
27
• Here, Car, Bus and Truck are concrete
classes
Multiple Inheritance
April 4, 2019
class
Information Technology
COMSATS Institute of
28
Mermaid
Example – Multiple Inheritance
COMSATS Institute of
April 4, 2019
29
Information Technology
Woman
Mermaid
Fish
Example – Multiple Inheritance
COMSATS Institute of
April 4, 2019
30
Information Technology
Amphibious Vehicles
Example – Multiple Inheritance
COMSATS Institute of
April 4, 2019
31
Information Technology
Example – Multiple Inheritance
April 4, 2019
Vehicle
Information Technology
COMSATS Institute of
Land Vehicle Water Vehicle
April 4, 2019
• Reduced understanding
Information Technology
COMSATS Institute of
• Duplicate features
33
Problem – Duplicate Features
Woman Fish
April 4, 2019
eat eat
… …
Information Technology
COMSATS Institute of
Mermaid
April 4, 2019
Woman Fish
eat eat
… …
Information Technology
COMSATS Institute of
Mermaid
Invoke eat
eat
operation of
… desired class
35
Problem – Duplicate Features
(Diamond Problem)
Vehicle
changeGear
April 4, 2019
Information Technology
COMSATS Institute of
Land Vehicle Water Vehicle
April 4, 2019
• Some languages disallow diamond hierarchy
Information Technology
COMSATS Institute of
37
April 4, 2019
References
• “Object Oriented Programming in C++”, by “Robert Lafore”,
published by Sams Publishing (The Waite Group). 4th ed. available
COMSATS Institute of
April 4, 2019
39
Information Technology