Sei sulla pagina 1di 39

April 27, 2018

CS 412:
Object Oriented

Alhamd Islamic University Islamabad


Programming
Fall 2016
1. Inheritance & Generalization

Friday, April 27, 2018


Salman Ahmed Rajpoot 1

Please turn OFF your Mobile Phones!


Quiz 1

April 27, 2018


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

Alhamd Islamic University


Islamabad
2
Recap – Inheritance
• Derived class inherits all the characteristics of the base class

April 27, 2018


• Besides inherited characteristics, derived class may have its
own unique characteristics

Alhamd Islamic University


Islamabad
• Major benefit of inheritance is reuse

3
Concepts Related with
Inheritance

April 27, 2018


• Generalization

• Subtyping (extension)

Alhamd Islamic University


Islamabad
• Specialization (restriction)

4
Generalization
• In OO models, some classes may have common characteristics

April 27, 2018


• We extract these features into a new class and inherit original
classes from this new class

Alhamd Islamic University


Islamabad
• This concept is known as Generalization

5
Example – Generalization
Line

April 27, 2018


color
vertices Circle
length color

Alhamd Islamic University


Islamabad
move vertices Triangle
setColor radius
color
getLength move
vertices
setColor
angle
computeArea
move
setColor
6
computeArea
Example – Generalization
Shape
color

April 27, 2018


vertices
move
setColor

Alhamd Islamic University


Islamabad
Circle Triangle
radius Line angle
computeArea length computeArea 7
getLength
Example – Generalization
Student
name

April 27, 2018


age Teacher
gender name
Doctor
program age
name

Alhamd Islamic University


Islamabad
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 27, 2018


age
gender
eat

Alhamd Islamic University


Islamabad
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 27, 2018


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

Alhamd Islamic University


Islamabad
• 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

April 27, 2018


compatible with the base class

• Behaviourally compatible means that base class can be

Alhamd Islamic University


Islamabad
replaced by the derived class

11
Person
name
age
gender

April 27, 2018


eats
Example – walks

Sub-typing

Alhamd Islamic University


Islamabad
(Extension) Student
program
studyYear
study 12

takeExam
Shape
color
vertices

April 27, 2018


setColor
Example – move

Sub-typing

Alhamd Islamic University


Islamabad
(Extension)
Circle
radius
computeCF
computeArea 13
Specialization (Restriction)
• Specialization means that derived class is behaviourally

April 27, 2018


incompatible with the base class

• Behaviourally incompatible means that base class can’t always

Alhamd Islamic University


Islamabad
be replaced by the derived class

14
Example – Specialization
(Restriction)
Person

April 27, 2018


age : [0..100]

setAge( a ) age = a

Alhamd Islamic University


Islamabad

Adult If age < 18 then


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

Example – Specialization
(Restriction)
IntegerSet

April 27, 2018



add( elem ) add element
… to the set

Alhamd Islamic University


Islamabad
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 27, 2018


by its base class

• Reasons for overriding

Alhamd Islamic University


Islamabad
• Provide behaviour specific to a derived class
• Extend the default behaviour
• Restrict the default behaviour
• Improve performance

17
Example – Specific Behaviour
Shape
color

April 27, 2018


vertices
draw
move

Alhamd Islamic University


Islamabad
setColor

Circle Triangle
radius Line angle
18
draw length draw
computeArea draw computeArea
Example – Extension
Window
width

April 27, 2018


height
open
close

Alhamd Islamic University


Islamabad
draw

DialogBox 1- Invoke Window’s


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

April 27, 2018



add( elem ) Add element
… to the set

Alhamd Islamic University


Islamabad
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
April 27, 2018 Alhamd Islamic University Islamabad 21
Abstract Classes

April 27, 2018


• An abstract class implements an abstract concept
• Main purpose is to be inherited by other classes
• Can’t be instantiated

Alhamd Islamic University


Islamabad
• Promotes reuse

22
Example – Abstract Classes
Person
name

April 27, 2018


age
gender

Alhamd Islamic University


Islamabad
eat
walk

Student Doctor
Teacher
23
• Here, Person is an abstract class
Example – Abstract Classes
Vehicle

April 27, 2018


color
model
accelerate

Alhamd Islamic University


Islamabad
applyBrakes

Car Truck
Bus
24
• Here, Vehicle is an abstract class
Concrete Classes
• A concrete class implements a concrete concept

April 27, 2018


• Main purpose is to be instantiated

Alhamd Islamic University


Islamabad
• Provides implementation details specific to the domain
context

25
Example – Concrete Classes

Person

April 27, 2018


Alhamd Islamic University
Islamabad
Student Doctor
program Teacher
studyYear
study
heldExam

26
• Here, Student, Teacher and Doctor are concrete classes
Example – Concrete Classes

Vehicle

April 27, 2018


Alhamd Islamic University
Islamabad
Car Truck
Bus
capacity
load
unload

27
• Here, Car, Bus and Truck are concrete
classes
Multiple Inheritance

April 27, 2018


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

Alhamd Islamic University


Islamabad
28
Mermaid
Example – Multiple Inheritance

Alhamd Islamic University


April 27, 2018
29

Islamabad
Example – Multiple Inheritance

April 27, 2018


Woman Fish

Alhamd Islamic University


Islamabad
Mermaid

30
Example – Multiple Inheritance

April 27, 2018


Alhamd Islamic University
Islamabad
Amphibious Vehicles 31
Example – Multiple Inheritance

April 27, 2018


Vehicle

Alhamd Islamic University


Islamabad
Land Vehicle Water Vehicle

Car Amphibious Vehicle Boat


32
Problems with Multiple
Inheritance

April 27, 2018


• Increased complexity

• Reduced understanding

Alhamd Islamic University


Islamabad
• Duplicate features

33
Problem – Duplicate Features

April 27, 2018


Woman Fish
eat eat
… …

Alhamd Islamic University


Islamabad
Mermaid

• Which eat operation Mermaid inherits?


34
Solution – Override the Common
Feature

April 27, 2018


Woman Fish
eat eat
… …

Alhamd Islamic University


Islamabad
Mermaid
Invoke eat
eat
operation of
… desired class
35
Problem – Duplicate Features
(Diamond Problem)
Vehicle

April 27, 2018


changeGear

Alhamd Islamic University


Islamabad
Land Vehicle Water Vehicle

Car Amphibious Vehicle Boat


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

April 27, 2018


• Some languages disallow diamond hierarchy

• Others provide mechanism to ignore characteristics from one side

Alhamd Islamic University


Islamabad
37
April 27, 2018
References
• “Object Oriented Programming in C++”, by “Robert Lafore”,
published by Sams Publishing (The Waite Group). 4th ed. available

Alhamd Islamic University Islamabad


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

Alhamd Islamic University


April 27, 2018
39

Islamabad

Potrebbero piacerti anche