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

Python Inheritance Types

The document provides a reference guide on the types of inheritance in Python, detailing five main types: Single, Multiple, Multilevel, Hierarchical, and Hybrid. Each type is illustrated with diagrams and code snippets to demonstrate how classes can inherit attributes and methods from parent classes. Additionally, it includes tips for inspecting inheritance and addresses the diamond problem in multiple inheritance scenarios.

Uploaded by

mahadalimughall
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 views7 pages

Python Inheritance Types

The document provides a reference guide on the types of inheritance in Python, detailing five main types: Single, Multiple, Multilevel, Hierarchical, and Hybrid. Each type is illustrated with diagrams and code snippets to demonstrate how classes can inherit attributes and methods from parent classes. Additionally, it includes tips for inspecting inheritance and addresses the diamond problem in multiple inheritance scenarios.

Uploaded by

mahadalimughall
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

Types of Inheritance in Python Reference Guide

Types of Inheritance in Python


Single · Multiple · Multilevel · Hierarchical · Hybrid

Inheritance lets one class (the child or derived class) reuse attributes and methods from another
class (the parent or base class). Python supports several flavors of inheritance, each shown in this
guide with a diagram and a runnable code snippet.

How to read the diagrams: arrows always point from the child class up toward the parent class. Filled boxes
are base (parent) classes, white boxes are derived (child) classes.

Page 1
Types of Inheritance in Python Reference Guide

1. Single Inheritance
A child class inherits from exactly one parent class. This is the simplest and most common form of
inheritance — the child gets all public attributes and methods of the parent, and can override or
extend them.

Animal

Dog

class Animal:
def speak(self):
print("Some sound")

class Dog(Animal): # Dog inherits from Animal


def bark(self):
print("Woof!")

d = Dog()
[Link]() # inherited from Animal -> 'Some sound'
[Link]() # defined in Dog -> 'Woof!'

Page 2
Types of Inheritance in Python Reference Guide

2. Multiple Inheritance
A child class inherits from two or more parent classes at once. It combines features from each parent.
When two parents define a method with the same name, Python uses the Method Resolution Order
(MRO) — based on the C3 linearization algorithm — to decide which one wins.

Father Mother

Child

class Father:
def skills(self):
print("Gardening, programming")

class Mother:
def skills(self):
print("Cooking, art")

class Child(Father, Mother): # multiple parents


pass

c = Child()
[Link]() # follows MRO -> Father's version wins
print(Child.__mro__)

Page 3
Types of Inheritance in Python Reference Guide

3. Multilevel Inheritance
Inheritance forms a chain: a class inherits from a parent, which itself inherits from a grandparent. The
most-derived class indirectly receives everything from the top of the chain.
Grandfather

Father

Son

class Grandfather:
def family_name(self):
print("Smith")

class Father(Grandfather): # level 2


def profession(self):
print("Engineer")

class Son(Father): # level 3


def hobby(self):
print("Football")

s = Son()
s.family_name() # from Grandfather
[Link]() # from Father
[Link]() # from Son

Page 4
Types of Inheritance in Python Reference Guide

4. Hierarchical Inheritance
Many child classes inherit from a single parent. Each child shares the parent's behavior but can
specialize it independently. This is common when modeling a family of related types — e.g. Vehicle
as the base, with Car, Bike, and Truck as children.

Vehicle

Car Bike Truck

class Vehicle:
def start(self):
print("Engine starting...")

class Car(Vehicle):
def wheels(self): print("4 wheels")

class Bike(Vehicle):
def wheels(self): print("2 wheels")

class Truck(Vehicle):
def wheels(self): print("6 wheels")

for v in (Car(), Bike(), Truck()):


[Link]() # inherited from Vehicle
[Link]() # specialized in each child

Page 5
Types of Inheritance in Python Reference Guide

5. Hybrid Inheritance
Hybrid inheritance is a combination of two or more types of inheritance — most often hierarchical +
multiple, which produces the classic diamond shape: one base class A, two intermediates B and C
that both inherit from A, and a final class D that inherits from both B and C. Python resolves the order
of method lookup using the C3 linearization algorithm.
A

B C

class A:
def greet(self): print("Hello from A")

class B(A):
def greet(self): print("Hello from B")

class C(A):
def greet(self): print("Hello from C")

class D(B, C): # diamond inheritance


pass

d = D()
[Link]() # 'Hello from B' (per MRO)
print(D.__mro__)
# (D, B, C, A, object)

Page 6
Types of Inheritance in Python Reference Guide

Summary & Tips


Type Pattern Typical use case

Single 1 child ← 1 parent Extending or specializing a single class

Multiple 1 child ← 2+ parents Mixing capabilities (mixins, interfaces)

Multilevel Chain: A → B → C Layered specialization across generations

Hierarchical 1 parent → many children Family of related variants sharing a base

Hybrid Combination (often diamond) Complex models combining the above

Useful tools to inspect inheritance

ClassName.__mro__ # tuple of classes in resolution order


[Link]() # same, but as a list
issubclass(D, A) # True if D inherits (directly or indirectly) from A
isinstance(obj, A) # True if obj is an instance of A or any subclass
super().method(...) # call the next method in the MRO chain

Tip — the diamond problem: when the same method exists in multiple parents, Python uses C3 linearization
to pick a single, predictable order. Always check __mro__ if you're unsure which method will be called.

Page 7

You might also like