0% found this document useful (0 votes)
3 views9 pages

Inheritance in Python

Uploaded by

24eg105q06
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)
3 views9 pages

Inheritance in Python

Uploaded by

24eg105q06
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

Inheritance in

Python
OOP CONCEPTS PYTHON PROGRAMMING
INTRODUCTION

What is Inheritance?
Inheritance is a concept where one class acquires the properties and methods
of another class. The class that gives the properties is called the Parent class Parent Class (Base)
(Base class), and the class that receives those properties is called the Child
Provides properties and methods to be inherited.
class (Derived class).
Example: Animal with eat() and sleep().

class Animal: # Parent class


def eat(self):
print("Animal is eating") Child Class (Derived)
def sleep(self): Receives properties from the parent and can add its
print("Animal is sleeping") own. Example: Dog with bark().

class Dog(Animal): # Child class


def bark(self):
print("Dog is barking")

d = Dog()
[Link]() # Animal is eating
[Link]() # Animal is sleeping
[Link]() # Dog is barking
ADVANTAGES & USES

Why Use Inheritance?

Code Reusability Easy Maintenance


Reuse existing code from the parent class without rewriting it, saving time and Changes made in the parent class automatically propagate to all child classes,
effort. simplifying updates.

Improves Program Structure Real-World Uses


Creates logical hierarchical relationships between classes, making code easier to Used in game development, banking systems, and software development to
understand and organize. model hierarchical relationships.
TYPE 1

Single Inheritance
Single Inheritance occurs when one child class inherits from only one parent class. The child
class gets the variables and methods of the parent and can also add its own features.
Dog (Child)
Inherits eat(); adds bark()
behavior
class Animal: # Parent class
Animal (Parent)
Defines eat() method shared def eat(self):
by children
print("Animal is eating")

class Dog(Animal): # Child class


def bark(self):
print("Dog is barking")

d = Dog()
[Link]() # inherited method
[Link]() # own method

Advantages
Code Reusability 3 Reuse code from the parent class
Less Duplication 3 No need to write the same code again
Easy to Understand 3 Simple one-to-one relationship
Easy Maintenance 3 Parent class changes benefit the child
TYPE 2

Multiple Inheritance
Multiple inheritance is a type of inheritance where one child class inherits properties and methods
from more than one parent class. The child class can access features from all parent classes.

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

class Mother:
def talents(self):
print("Mother: Cooking")
Father (Parent Child Mother (Parent
class Child(Father, Mother): (inherits A
A) B)
def hobbies(self): & B)
print("Child: Playing")

c = Child()
[Link]() # from Father
[Link]() # from Mother
[Link]() # own method

Father ³ Parent A
Provides skills() method

Mother ³ Parent B
Provides talents() method

Child ³ Derived
Inherits from both and adds hobbies()
TYPE 3

Multilevel Inheritance
Multilevel inheritance is when a class inherits from another class, and then another class inherits from that child
class, forming a chain of inheritance across multiple levels.

01
class Animal:
def eat(self): Animal ³ Grandparent
print("Animal eats food")
Top-level class providing eat()
class Dog(Animal):
def bark(self): 02
print("Dog barks")
Dog ³ Parent
class Puppy(Dog): Inherits from Animal, adds bark()
def weep(self):
print("Puppy weeps") 03

p = Puppy() Puppy ³ Child


[Link]() # from Animal
Inherits from Dog (and Animal), adds weep()
[Link]() # from Dog
[Link]() # own method
Puppy can use eat() from Animal, bark() from Dog, and
weep() from itself 4 enabling logical hierarchy like
LivingThing ³ Animal ³ Dog.
TYPE 4

Hierarchical Inheritance
In Hierarchical Inheritance, multiple child classes inherit from one single
parent class. Each child class shares the base features of the parent but BankAccount ³ Parent
can add its own unique methods.
Provides common methods like deposit() and withdraw() shared by all
child classes.

SavingsAccount
Inherits BankAccount;
adds interest operations SavingsAccount ³ Child 1
BankAcc CurrentAccount Inherits from BankAccount and adds interest calculation features.
ount Inherits BankAccount;
(Parent) supports overdraft and
checks

FixedDepositAc
count CurrentAccount ³ Child 2
Inherits BankAccount; fixed
term and higher interest
Inherits from BankAccount and adds overdraft features.

FixedDepositAccount ³ Child 3
Inherits from BankAccount and adds fixed-term deposit features.
TYPE 5

Hybrid Inheritance
Hybrid Inheritance is a combination of two or more types of inheritance. It
merges patterns like single, multiple, and hierarchical inheritance to model
complex real-world relationships. TeachingAssistant
Derived class combining
Example: Teacher and Student both inherit from Person. Then TeachingAssistant Teacher and Student features
inherits from both Teacher and Student.

Teacher &
Key Point Student
Separate child classes
Hybrid inheritance combines the features of multiple inheritance inheriting from Person
types, so a class like TeachingAssistant can access methods from Person
Top parent class representing
Person, Teacher, and Student. common attributes

Person Teacher + Student


Top-level parent class Inherit from Person

TeachingAssistant
Inherits from both Teacher & Student
CONCLUSION

Summary: Types of Inheritance in Python


Single Inheritance
1 One child class inherits from one parent class. Simplest and most common form. Example: Animal ³ Dog

Multiple Inheritance
2 One child class inherits from two or more parent classes. Example: Father + Mother ³ Child

Multilevel Inheritance
3 Inheritance happens in a chain across multiple levels. Example: Animal ³ Dog ³ Puppy

Hierarchical Inheritance
4 Multiple child classes inherit from one parent class. Example: BankAccount ³ Savings, Current, Fixed

Hybrid Inheritance
5 Combination of multiple types of inheritance to model complex relationships. Example: Person ³ Teacher + Student ³ TeachingAssistant

Key Takeaway
Inheritance is one of the four pillars of Object-Oriented Programming. It promotes code reusability, reduces redundancy, and enables logical hierarchical modeling of real-world
systems 4 making programs easier to build, understand, and maintain.

You might also like