0% found this document useful (0 votes)
29 views20 pages

Week11 Chapter 11 Inheritance

Uploaded by

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

Week11 Chapter 11 Inheritance

Uploaded by

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

Haliç University

Faculty of Engineering

CEN121

COMPUTER PROGRAMMING 1

Chapter 11

’’ Inheritance’’
Lecturer: Asst.Prof. Zehra Merve Cinan
TOPICS
• Introduction to Inheritance
• Polymorphism

Copyright © 2022 Pearson Education, Ltd. All Rights Reserved.

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.

 In the real world, many objects are a specialized version of more


general objects.

*Example: grasshoppers and bees are specialized types of insect.


 In addition to the general insect characteristics, they have
unique characteristics:
• Grasshoppers can jump.
• Bees can sting, make honey, and build hives.
Copyright © 2022 Pearson Education, Ltd. All Rights Reserved.

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

Copyright © 2022 Pearson Education, Ltd. All Rights Reserved.

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.

*The specialized object has all the characteristics of the


general object plus unique characteristics.

*Example: Rectangle is a shape.


 Daisy is a flower.
 Inheritance: used to create an “is a” relationship between
classes.

 Superclass (base class): a general class.

 Subclass (derived class): a specialized class.

*An extended version of the superclass.


 Inherits attributes and methods of the superclass.
 New attributes and methods can be added.
Copyright © 2022 Pearson Education, Ltd. All Rights Reserved.

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.

*This can be the attributes for the base class.

 In addition:
*Car has a number of doors.

*Pickup truck has a drive type.

*SUV has a passenger capacity.

 In a class definition for a subclass:


*To indicate inheritance, the superclass name is placed in parentheses after
subclass name.
 Example: class Car(Automobile):
*The initializer method of a subclass calls the initializer method of the
superclass and then initializes the unique data attributes.

*Add method definitions for unique methods.

Copyright © 2022 Pearson Education, Ltd. All Rights Reserved.

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

 In UML diagram, show inheritance by


drawing a line with an open arrowhead
from subclass to superclass.

Copyright © 2022 Pearson Education, Ltd. All Rights Reserved.

11
CEN121- COMPUTER PROGRAMMING 1/ 2023-2024 FALL
Polymorphism
 Polymorphism: an object’s ability to take different forms.

 Essential ingredients of polymorphic behavior:

*Ability to define a method in a superclass and override it in a subclass.


 Subclass defines a method with the same name.
*Ability to call the correct version of overridden method depending on the
type of object that called for it.

 In previous inheritance examples showed how to override the


__init__ method.

*Called superclass __init__ method and then added onto that.

 The same can be done for any other method.

*The method can call the superclass equivalent and add to it, or do
something completely different.

Copyright © 2022 Pearson Education, Ltd. All Rights Reserved.

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()

Copyright © 2022 Pearson Education, Ltd. All Rights Reserved.

14
CEN121- COMPUTER PROGRAMMING 1/ 2023-2024 FALL
The isinstance Function

 Polymorphism provides great flexibility when designing


programs.

 AttributeError exception: raised when a method is


receives an object which is not an instance of the right
class.

 isinstance function: determines whether object is an


instance of a class.

*Format: isinstance(object, class).

Copyright © 2022 Pearson Education, Ltd. All Rights Reserved.

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

Copyright © 2022 Pearson Education, Ltd. All Rights Reserved.

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

[email protected]

CEN121- COMPUTER PROGRAMMING 1/ 2023-2024 FALL 20

You might also like