OOP # 3 Inheritance

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

7/5/2018

Object Oriented Programming


Dr. Muzammil Khan

Assistant Professor
Department of Computer Science

Office E – 12

Chapter 3

Inheritance

Object Oriented Programming


2

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

 Besides inherited characteristics,


 Derived class may have its own unique characteristics

 Inherited class B from class A


 A is KIND OF B
 Know as “A KIND OF” Relationship

Object Oriented Programming


4

2
7/5/2018

Example – Inheritance

Person

Student Doctor
Teacher

Object Oriented Programming


5

Example – Inheritance (Cont…)


Person
name
age
gender
eat
walk

Student Teacher Doctor


program designation designation
studyYear salary salary
study teach checkUp
heldExam takeExam prescribe
Object Oriented Programming
6

3
7/5/2018

Example – Inheritance (Cont…)


Shape
color
coord
draw
rotate
setColor

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

Object Oriented Programming


9

Reuse with Inheritance


 Main purpose of inheritance is reuse

 We can easily add new classes by inheriting from existing


classes
 Select an existing class closer to the desired functionality
 Create a new class and inherit it from the selected class
 Add to and/or modify the inherited functionality

Object Oriented Programming


10

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

Example – Reuse (Cont…)


Person
name
age
gender
eat
walk

Student Teacher Doctor


program designation designation
studyYear salary salary
study teach checkUp
heldExam takeExam prescribe
Object Oriented Programming
12

6
7/5/2018

Example – Reuse (Cont…)


Person
name
age
gender
eat
walk

Student Teacher Doctor


program designation designation
studyYear salary salary
study teach checkUp
heldExam takeExam prescribe
Object Oriented Programming
13

Inheritance Related Concepts


 Three closely related concepts are

1. Generalization

2. Subtyping (extension)

3. Specialization (restriction)

Object Oriented Programming


14

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

 Draw individual class with characteristics


 Generalized all three classes into one general class “Person”

 For guidance use previous example

Object Oriented Programming


17

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

Object Oriented Programming


19

Specialization (Restriction)
 Specialization means that derived class is behaviourally
incompatible with the base class

 Behaviourally incompatible means that base class can’t


always be replaced by the derived class

 Example
 Next slide

Object Oriented Programming


20

10
7/5/2018

Example - Specialization
Person
age : [0..100]

setAge( a ) age = a

Adult If age < 18 then


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

Object Oriented Programming
21

Example – Specialization (Cont...)


IntegerSet

add(elem) add element
… to the set

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

 Reasons for overriding


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

Object Oriented Programming


23

Example – Specific Behaviour


Shape
color
vertices
draw
move
setColor

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

Object Oriented Programming


26

13
7/5/2018

Example – Improve Performance


 Class Circle overrides rotate operation of class Shape with
a Null operation.

Shape
color Circle
coord radius
draw draw
rotate rotate
setColor

Object Oriented Programming


27

Abstract Classes
 An abstract class implements an abstract concept
 For Example
 Person is abstract concept for student, teacher, doctor etc.

 Main purpose is to be inherited by other classes

 Can’t be instantiated
 Can not take object of it

 Promotes reuse
 Encourage reusability

Object Oriented Programming


28

14
7/5/2018

Example – Abstract Classes


Person
name
age
gender
eat
walk

Student Doctor
Teacher
 Here, Person is an abstract class
Object Oriented Programming
29

Example – Abstract Classes

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

 Main purpose is to be instantiated


 Can take object of it

 Provides implementation details specific to the domain


context

Object Oriented Programming


31

Example – Concrete Classes

Person

Student Doctor
program Teacher
studyYear
study
heldExam

 Here, Student, Teacher and Doctor are concrete classes

Object Oriented Programming


32

16
7/5/2018

Example – Concrete Classes


Vehicle

Car Truck
Bus
capacity
load
unload

 Here, Car, Bus and Truck are concrete classes

Object Oriented Programming


33

End of Chapter
 Lab Task
 Practice the concept of classes and inheritance with related
concepts

 You may have quiz next week

Object Oriented Programming


34

17

You might also like