Week11 Chapter 11 Inheritance
Week11 Chapter 11 Inheritance
Faculty of Engineering
CEN121
COMPUTER PROGRAMMING 1
Chapter 11
’’ Inheritance’’
Lecturer: Asst.Prof. Zehra Merve Cinan
TOPICS
• Introduction to Inheritance
• Polymorphism
2
CEN121- COMPUTER PROGRAMMING 1/ 2023-2024 FALL
Introduction to Inheritance
Inheritance allows us to define a class that inherits all the methods
and properties from another class.
*Parent class is the class being inherited from, also called base class.
*Child class is the class that inherits from another class, also called
derived class.
3
CEN121- COMPUTER PROGRAMMING 1/ 2023-2024 FALL
Create a Parent Class
Any class can be a parent class, so the syntax is the same as creating any other class.
If we want to create a class named Person, with firstname and lastname properties, and a printname method:
class Person:
def __init__(self, fname, lname):
self.firstname = fname
self.lastname = lname
def printname(self):
print(self.firstname, self.lastname)
#Use the Person class to create an object, and then execute the printname method:
x = Person("John", "Doe")
x.printname()
John Doe
4
CEN121- COMPUTER PROGRAMMING 1/ 2023-2024 FALL
Create a Child Class
To create a class that inherits the functionality from another class, send the parent class as a parameter when creating the
child class.
Note: Use the pass keyword when you do not want to add any other properties or methods to the class.
If we want to create a class named Student, which will inherit the properties and methods from the Person class:
class Person:
def __init__(self, fname, lname):
self.firstname = fname
self.lastname = lname
def printname(self):
print(self.firstname, self.lastname)
class Student(Person):
pass
x = Student("Mike", "Olsen")
x.printname()
Mike Olsen
Copyright © 2022 Pearson Education, Ltd. All Rights Reserved.
5
CEN121- COMPUTER PROGRAMMING 1/ 2023-2024 FALL
Inheritance and the “Is a” Relationship
“Is a” relationship: exists when one object is a specialized
version of another object.
6
CEN121- COMPUTER PROGRAMMING 1/ 2023-2024 FALL
For example, need to create classes for cars, pickup trucks, and SUVs.
All are automobiles.
*Have a make, year model, mileage, and price.
In addition:
*Car has a number of doors.
7
CEN121- COMPUTER PROGRAMMING 1/ 2023-2024 FALL
Copyright © 2022 Pearson Education, Ltd. All Rights Reserved.
8
CEN121- COMPUTER PROGRAMMING 1/ 2023-2024 FALL
Copyright © 2022 Pearson Education, Ltd. All Rights Reserved.
9
CEN121- COMPUTER PROGRAMMING 1/ 2023-2024 FALL
Copyright © 2022 Pearson Education, Ltd. All Rights Reserved.
10
CEN121- COMPUTER PROGRAMMING 1/ 2023-2024 FALL
Inheritance in UML Diagrams
11
CEN121- COMPUTER PROGRAMMING 1/ 2023-2024 FALL
Polymorphism
Polymorphism: an object’s ability to take different forms.
*The method can call the superclass equivalent and add to it, or do
something completely different.
12
CEN121- COMPUTER PROGRAMMING 1/ 2023-2024 FALL
Copyright © 2022 Pearson Education, Ltd. All Rights Reserved.
13
CEN121- COMPUTER PROGRAMMING 1/ 2023-2024 FALL
So far we have created a child class that inherits the properties and methods from its parent.
We want to add the __init__() function to the child class (instead of the pass keyword).
*Note: The __init__() function is called automatically every time the class is being used to create a new object.
When you add the __init__() function, the child class will no longer inherit the parent's __init__() function.
*Note: The child's __init__() function overrides the inheritance of the parent's __init__() function.
To keep the inheritance of the parent's __init__() function, add a call to the parent's __init__() function:
class Person:
def __init__(self, fname, lname):
self.firstname = fname
self.lastname = lname
def printname(self):
print(self.firstname, self.lastname)
Mike Olsen
class Student(Person):
def __init__(self, fname, lname):
Person.__init__(self, fname, lname)
x = Student("Mike", "Olsen")
x.printname()
14
CEN121- COMPUTER PROGRAMMING 1/ 2023-2024 FALL
The isinstance Function
15
CEN121- COMPUTER PROGRAMMING 1/ 2023-2024 FALL
Copyright © 2022 Pearson Education, Ltd. All Rights Reserved.
16
CEN121- COMPUTER PROGRAMMING 1/ 2023-2024 FALL
Copyright © 2022 Pearson Education, Ltd. All Rights Reserved.
17
CEN121- COMPUTER PROGRAMMING 1/ 2023-2024 FALL
Copyright © 2022 Pearson Education, Ltd. All Rights Reserved.
18
CEN121- COMPUTER PROGRAMMING 1/ 2023-2024 FALL
Summary
This chapter covered:
• Inheritance, including:
“Is a” relationships
Subclasses and superclasses
Defining subclasses and initializer methods
Depicting inheritance in UML diagrams
• Polymorphism
• The isinstance function
19
CEN121- COMPUTER PROGRAMMING 1/ 2023-2024 FALL
Asst.Prof. Zehra Merve Cinan
Faculty of Engineering
Department of Electrical and Electronics Engineering,
8th Block, Floor: 2
Office: 2521