Professional Documents
Culture Documents
OOP # 3 Inheritance
OOP # 3 Inheritance
OOP # 3 Inheritance
Assistant Professor
Department of Computer Science
Office E – 12
Chapter 3
Inheritance
1
7/5/2018
Inheritance
A child inherits characteristics of its parents
Besides inherited characteristics
A child may have its own unique characteristics
Similarly, in programming
A class can carry characteristics of another class
Besides,
Has its own characteristics
For Example
If a class B inherits from class A then
It contains all the characteristics (information structure and
behaviour) of class A
Object Oriented Programming
3
Inheritance (Cont…)
Class that has been inherited by another class
Known as base class, parent class or super class
Class that carry characteristics of another class
Known as derived class, child class or sub class
2
7/5/2018
Example – Inheritance
Person
Student Doctor
Teacher
3
7/5/2018
Circle Triangle
radius Line angle
draw length draw
computeArea draw computeArea
7
Inheritance – Advantages
Reusability
Facility to use public methods of base class without
rewriting the same
Extensibility
Extending the base class logic as per business logic of the
derived class
Data hiding
Base class can decide to keep some data private so that it
cannot be altered by the derived class
Overriding
With inheritance, we will be able to override the methods of
the base class so that meaningful implementation of the
base class method can be designed in the derived class.
Object Oriented Programming
8
4
7/5/2018
Inheritance – Disadvantages
Homework
Task
In next class
5
7/5/2018
Example – Reuse
Shape
color
coord
draw
rotate
setColor
Circle Triangle
radius Line angle
draw length draw
computeArea draw computeArea
Object Oriented Programming
11
6
7/5/2018
1. Generalization
2. Subtyping (extension)
3. Specialization (restriction)
7
7/5/2018
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
Triangle
Line Circle
color color color
vertices vertices vertices
length radius angle
move move move
setColor setColor setColor
getLength computeArea computeArea
Object Oriented Programming
15
Example – Generalization
Shape
color
vertices
move
setColor
Circle Triangle
radius Line angle
computeArea length computeArea
getLength
Object Oriented Programming
16
8
7/5/2018
Class Task
How we can generalized classes for
Student
Teacher
Doctor
Sub-typing (Extension)
Sub-typing means that derived class is behaviourally
compatible with the base class
Behaviourally compatible means that base class can be
replaced by the derived class
Examples
Person Student
name
program
age
studyYear
gender
eats study
walks takeExam
Object Oriented Programming
18
9
7/5/2018
Example – Sub-typing
Shape Circle
color radius
vertices
computeCF
setColor computeArea
move
Specialization (Restriction)
Specialization means that derived class is behaviourally
incompatible with the base class
Example
Next slide
10
7/5/2018
Example - Specialization
Person
age : [0..100]
…
setAge( a ) age = a
…
If elem < 1
then
NaturalSet error
… else
add (elem) add
… element to
the set
Object Oriented Programming
22
11
7/5/2018
Overriding
A class may need to override the default behaviour
provided by its base class
Circle Triangle
radius Line angle
draw length draw
computeArea draw computeArea
Object Oriented Programming
24
12
7/5/2018
Example – Extension
Window
width
height
open
close
draw
DialogBox 1- Invoke
controls Window’s draw
enable 2- draw the
draw dialog box
Object Oriented Programming
25
Example – Restriction
IntegerSet
…
add( elem ) Add element
… to the set
If elem < 1
then
NaturalSet give error
… else
add( elem ) Add element
… to the set
13
7/5/2018
Shape
color Circle
coord radius
draw draw
rotate rotate
setColor
Abstract Classes
An abstract class implements an abstract concept
For Example
Person is abstract concept for student, teacher, doctor etc.
Can’t be instantiated
Can not take object of it
Promotes reuse
Encourage reusability
14
7/5/2018
Student Doctor
Teacher
Here, Person is an abstract class
Object Oriented Programming
29
Vehicle
color
model
accelerate
applyBrakes
Car Truck
Bus
Here, Vehicle is an abstract class
Object Oriented Programming
30
15
7/5/2018
Concrete Classes
A concrete class implements a concrete concept
For Example
Student, Teacher, Doctor etc., are concrete concepts of
abstract concept Person
Person
Student Doctor
program Teacher
studyYear
study
heldExam
16
7/5/2018
Car Truck
Bus
capacity
load
unload
End of Chapter
Lab Task
Practice the concept of classes and inheritance with related
concepts
17