0% found this document useful (0 votes)
52 views39 pages

CSC241: Object Oriented Programming: Fall 2016

The document discusses object-oriented programming concepts such as inheritance, generalization, subtyping, specialization, overriding, and provides examples to illustrate each concept. It defines inheritance as a derived class inheriting characteristics from its base class. Generalization involves extracting common characteristics into a new base class. Subtyping means a derived class is behaviorally compatible with its base class, while specialization means a derived class is behaviorally incompatible. Overriding allows a derived class to modify or extend the behavior it inherits.

Uploaded by

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

CSC241: Object Oriented Programming: Fall 2016

The document discusses object-oriented programming concepts such as inheritance, generalization, subtyping, specialization, overriding, and provides examples to illustrate each concept. It defines inheritance as a derived class inheriting characteristics from its base class. Generalization involves extracting common characteristics into a new base class. Subtyping means a derived class is behaviorally compatible with its base class, while specialization means a derived class is behaviorally incompatible. Overriding allows a derived class to modify or extend the behavior it inherits.

Uploaded by

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

April 4, 2019

CSC241:
Object Oriented

COMSATS Institute of Information Technology


Programming
Fall 2016
1. Inheritance & Generalization

Thursday, April 4, 2019


Farhan Aadil 1

Please turn OFF your Mobile Phones!


Quiz 1

• Give a definition of an Object in object-oriented programming

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

Student Teacher Doctor


program designation designation
studyYear salary salary
study teach checkUp 9

heldExam takeExam prescribe


Sub-typing & Specialization
• We want to add a new class to an existing model

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

Adult If age < 18 then


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

Example – Specialization
(Restriction)
IntegerSet

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

• A class may need to override the default behaviour provided

April 4, 2019
by its base class

• Reasons for overriding

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

DialogBox 1- Invoke Window’s


controls draw
enable 2- draw the dialog 19
draw box
Example – Restriction
IntegerSet

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

• We may want to reuse characteristics of more than one parent

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

Car Amphibious Vehicle Boat


32
Problems with Multiple
Inheritance
• Increased complexity

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

• Which eat operation Mermaid inherits?


34
Solution – Override the Common
Feature

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

Car Amphibious Vehicle Boat


• Which changeGear operation Amphibious Vehicle inherits? 36
Solution to Diamond Problem

April 4, 2019
• Some languages disallow diamond hierarchy

• Others provide mechanism to ignore characteristics from one side

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 Information Technology


in soft form.
• “Object Oriented Programming Using C++” by “Joyce Farrell” ,
published by Course Technology, Cengage Learning. 4th ed. available
in soft form
• National University of Computer & Emerging Sciences
[www.nu.edu.pk]
• Virtual University of Pakistan [ocw.vu.edu.pk]
• Open Courseware Consortium
[http://www.ocwconsortium.org/en/courses]
38
Thanks

COMSATS Institute of
April 4, 2019
39

Information Technology

You might also like