0% found this document useful (0 votes)
2 views22 pages

Chapter 03

The document covers various types of inheritance in Python, including single, multiple, multilevel, hierarchical, and hybrid inheritance. It provides definitions, examples, and explanations of how inheritance works, along with potential issues like the diamond problem. Additionally, it discusses the importance of UML diagrams in planning inheritance structures before coding.
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)
2 views22 pages

Chapter 03

The document covers various types of inheritance in Python, including single, multiple, multilevel, hierarchical, and hybrid inheritance. It provides definitions, examples, and explanations of how inheritance works, along with potential issues like the diamond problem. Additionally, it discusses the importance of UML diagrams in planning inheritance structures before coding.
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

Course Name: Data Sciences and Artificial

Intelligence

Chapter 3: Inheritance in Python


Course Code: 0142321

Dr. Ibrahim Atoum


[Link]@[Link]

Office No.: 240


Different types of Python Inheritance

1. Single inheritance
2. Multiple inheritances
3. Multilevel inheritance
4. Hierarchical inheritance
5. Hybrid inheritance

Dr. Ibrahim Atoum 2


Single Inheritance
When a child class inherits from only one parent class, it is called
single inheritance.

one parent → one child

Dr. Ibrahim Atoum 3


Example: Single Inheritance

Dr. Ibrahim Atoum 4


Multiple Inheritance
When a child class inherits from multiple parent classes, it is called
multiple inheritances.

Dr. Ibrahim Atoum 5


Example: Multiple Inheritance
class Parent1:
def method1(self):
print("Method from Parent1")

class Parent2:
def method2(self):
print("Method from Parent2")

class Parent3:
def method3(self):
print("Method from Parent3")

class Child(Parent1, Parent2, Parent3):


def method4(self):
print("Method from Child")

c = Child()
c.method1()
c.method2() Method from Parent1
c.method3() Method from Parent2
c.method4() Method from Parent3
Method from Child

Dr. Ibrahim Atoum 6


Multilevel Inheritance
When we have a child and grandchild relationship. This means that
a child class will inherit from its parent class, which in turn is
inheriting from its parent class.

multilevel inheritance means a chain of inheritance, where a class inherits from a parent,
which in turn inherits from its own parent, and so on.

Dr. Ibrahim Atoum 7


Example: Multilevel Inheritance

Dr. Ibrahim Atoum 8


Hierarchical Inheritance
More than one derived class can be created from a single
base.

Dr. Ibrahim Atoum 9


Example: Hierarchical Inheritance

Dr. Ibrahim Atoum 10


Hybrid Inheritance
This form combines more than one form of inheritance. Basically, it
is a blend of more than one type of inheritance.

Dr. Ibrahim Atoum 11


Example: Hybrid Inheritance

Dr. Ibrahim Atoum 12


?

In Multiple Inheritance, what determines the order in which Python


searches for methods when a method is called?
A) Alphabetical order of parent class names
B) The order specified in the class definition
C) Random selection by Python interpreter
D) The order in which methods were defined

Answer: B) The order specified in the class definition


Explanation: Python uses Method Resolution Order (MRO) which
follows the order of parent classes in the inheritance list.

Dr. Ibrahim Atoum 13


?

In the Multiple Inheritance example from the slides, if Parent1 and Parent2 both
had a method called common_method(), which one would be called
when c.common_method() is invoked?
A) Parent1's method (first in inheritance list)
B) Parent2's method (last in inheritance list)
C) A random selection
D) Error due to ambiguity

Answer: A) Parent1's method (first in inheritance list)


Explanation: Python's MRO follows the order in the inheritance tuple, so
it checks Parent1 first.

Dr. Ibrahim Atoum 14


?
What is the main potential drawback of Multiple Inheritance that developers need
to be cautious about?
A) It makes code run slower
B) It can lead to the "diamond problem" and method resolution conflicts
C) It requires more memory
D) It only works with simple data types

Answer: B) It can lead to the "diamond


problem" and method resolution
conflicts
Explanation: The diamond problem
occurs when a class inherits from two
classes that have a common ancestor.

Dr. Ibrahim Atoum 15


?

In Hierarchical Inheritance, what happens if you modify a method


in the base class?
A) Only the first child class is affected
B) No child classes are affected
C) All derived classes that inherit from that base are affected
D) You cannot modify methods in base classes

Answer: C) All derived classes that inherit from that base are
affected
Explanation: Changes in the base class propagate to all child classes in
hierarchical inheritance.

Dr. Ibrahim Atoum 16


?

In Hierarchical Inheritance, what happens if you modify a method


in the base class?
A) Only the first child class is affected
B) No child classes are affected
C) All derived classes that inherit from that base are affected
D) You cannot modify methods in base classes

Answer: C) All derived classes that inherit from that base are
affected
Explanation: Changes in the base class propagate to all child classes in
hierarchical inheritance.

Dr. Ibrahim Atoum 17


?
Which inheritance type would be most appropriate for modeling:
"Car and Motorcycle both inherit from Vehicle, while SportsCar
inherits from Car"?
A) Single Inheritance only
B) Multiple Inheritance
C) Hybrid Inheritance (Single + Hierarchical)
D) Pure Multilevel Inheritance
Answer: C) Hybrid Inheritance (Single + Hierarchical)
Explanation: This represents a combination - Car and Motorcycle show
hierarchical inheritance from Vehicle, while SportsCar shows single
inheritance from Car.

Dr. Ibrahim Atoum 18


?
What would happen if you tried to create this inheritance
structure: class Child(Parent1, Parent1):?
A) Valid - creates two references to the same parent
B) Error - duplicate base class
C) Valid - but only one parent is actually inherited
D) Valid - creates a stronger inheritance bond

Answer: B) Error - duplicate base class


Explanation: Python doesn't allow the same class to appear multiple
times in the inheritance list.

Dr. Ibrahim Atoum 19


?
What is the primary purpose of creating UML diagrams before writing
inheritance code?
A) To make the presentation look more professional
B) To visually plan and validate the inheritance hierarchy before implementation
C) Because Python requires UML diagrams to compile
D) To increase the number of lines in the project

Answer: B) To visually plan and validate the inheritance hierarchy before implementation
Explanation: UML helps design and communicate the class relationships clearly before coding

Dr. Ibrahim Atoum 20


?
In Multilevel Inheritance, if all classes in the chain have an __init__ method, which one(s) get called
when creating an instance of the bottom-most class?

class Grandparent: class Grandparent:


def __init__(self): def __init__(self):
print("Grandparent init") print("Grandparent init")
class Parent(Grandparent): class Parent(Grandparent):
def __init__(self): def __init__(self):
print("Parent init") super().__init__()
class Child(Parent): print("Parent init")
def __init__(self): class Child(Parent):
print("Child init") def __init__(self):
c = Child() super().__init__()
print("Child init")
A c = Child()

A) Only the bottom-most class's __init__ C


B) Only the top-most class's __init__
C) All __init__ methods in the inheritance chain
D) No __init__ methods are called automatically

Dr. Ibrahim Atoum 21


Dr. Ibrahim Atoum 22

You might also like