SlideShare a Scribd company logo
Inheritance in Object-Oriented
Programming (OOP)
Understanding Classes, Objects, and Inheritance
in Python
Abigail Judith
Date
Introduction to OOP and Inheritance
Object-Oriented Programming (OOP):
- Focuses on objects that interact with each other.
- Concepts include classes, objects, inheritance, polymorphism,
encapsulation, and abstraction.
Inheritance:
- Inheritance is the mechanism that allows one class (child) to
inherit attributes and methods from another class (parent).
- Benefits of inheritance: Reusability, Extensibility, Improved code
organization.
Why Use Inheritance?
- Reusability: Child classes can reuse code from
the parent class, avoiding duplication.
- Extensibility: Child classes can extend the
functionality of parent classes.
- Simplifies Maintenance: Changes to the parent
class automatically reflect in the child classes.
Types of Inheritance
- Single Inheritance: A child class inherits from one parent class.
Example: class Dog(Animal):
- Multiple Inheritance: A child class inherits from more than one
parent class.
Example: class Dog(Mammal, Animal):
- Multilevel Inheritance: A class inherits from a class, which is itself
derived from another class.
Example: class Puppy(Dog):
Inheritance Syntax
Basic Syntax:
class ParentClass:
def __init__(self):
pass
def method_in_parent(self):
pass
class ChildClass(ParentClass): # Inherits from ParentClass
def __init__(self):
super().__init__() # Calls the constructor of ParentClass
pass
def method_in_child(self):
pass
Creating Objects for the Class
Class Object Instantiation:
When a class is defined, you can create objects (instances) of the class to
access its methods and attributes.
Example:
class Animal:
def __init__(self, name):
self.name = name
# Creating an object of Animal class
animal = Animal("Lion")
print(animal.name) # Output: Lion
Demonstrating Inheritance with Example
Parent Class:
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
print(f"{self.name} makes a sound")
Child Class:
class Dog(Animal):
def __init__(self, name, breed):
super().__init__(name)
self.breed = breed
def speak(self):
print(f"{self.name} barks")
Creating Objects for the Child Class:
dog = Dog("Buddy", "Golden Retriever")
dog.speak() # Output: Buddy barks
# Parent class
class Parent:
def __init__(self):
self.parent_var = "I am a Parent variable"
def parent_method(self):
print("This is a method from Parent class")
# First Child class
class Child1(Parent):
def child1_method(self):
print("This is a method from Child1 class")
# Second Child class
class Child2(Parent):
def child2_method(self):
print("This is a method from Child2 class")
# Creating objects of child classes
obj1 = Child1()
obj2 = Child2()
# Accessing Parent class properties and methods
print(obj1.parent_var) # Accessing Parent's variable
obj1.parent_method() # Calling
class Animal:
def move(self):
print('Animals can move')
class Bird:
def fly(self):
print('Birds can fly')
class Bat(Animal, Bird):
def sound(self):
print('Bats make high-pitched sounds')
bat = Bat()
bat.move()
bat.fly()
bat.sound()
Python follows MRO when resolving
methods.
- The `__mro__` attribute shows the method
lookup order.
- Example:
print(Bat.__mro__)
Output:
(<class 'Bat'>, <class 'Animal'>, <class 'Bird'>,
<class 'object'>)
Method Overriding in Inheritance
Definition: The child class can provide its own implementation of a method that is already
defined in the parent class.
Example:
class Animal:
def speak(self):
print("Animal makes a sound")
class Dog(Animal):
def speak(self):
print("Dog barks")
animal = Animal()
animal.speak() # Output: Animal makes a sound
dog = Dog()
dog.speak() # Output: Dog barks
Super Function and Inheritance
The super() function is used to call methods from the parent class.
This is particularly useful in the child class constructor to call the parent class's __init__()
method.
Example:
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
print(f"{self.name} makes a sound")
class Dog(Animal):
def __init__(self, name, breed):
super().__init__(name) # Call the parent class constructor
self.breed = breed
def speak(self):
print(f"{self.name} barks")
dog = Dog("Buddy", "Golden Retriever")
dog.speak() # Output: Buddy barks
Private and Protected Attributes in
Inheritance
Private Attributes:
- Private attributes (using __) are not accessible outside the class, even in subclasses.
Example:
class Animal:
def __init__(self, name):
self.__name = name # Private attribute
def get_name(self):
return self.__name
class Dog(Animal):
def __init__(self, name):
super().__init__(name)
dog = Dog("Buddy")
print(dog.get_name()) # Access private attribute via public method
Access Modifiers in Inheritance
• Public Attributes: Accessible from anywhere.
• Protected Attributes: Indicated by a single
underscore (_), meant for internal use within a
class or subclass.
• Private Attributes: Indicated by double
underscores (__), not directly accessible
outside the class or subclass.
Inheritance in Action - Example
Parent Class:
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
print(f"{self.name} makes a sound")
Child Class:
class Dog(Animal):
def speak(self):
print(f"{self.name} barks")
class Cat(Animal):
def speak(self):
print(f"{self.name} meows")
dog = Dog("Buddy")
cat = Cat("Kitty")
dog.speak() # Buddy barks
cat.speak() # Kitty meows
Conclusion
- Inheritance allows one class to acquire
attributes and methods from another.
- It promotes code reuse and provides a way to
extend or modify functionality.
- You can override methods in the child class and
use super() to call the parent class's methods.
Q&A
• Open floor for questions and clarifications.

More Related Content

Similar to Inheritance_in_OOP_using Python Programming (20)

PPTX
Inheritance
JayanthiNeelampalli
 
PPTX
arthimetic operator,classes,objects,instant
ssuser77162c
 
PDF
python note.pdf
Nagendra504676
 
PPTX
Inheritance_Polymorphism_Overloading_overriding.pptx
MalligaarjunanN
 
PPTX
Class and Objects in python programming.pptx
Rajtherock
 
PPTX
Python oop - class 2 (inheritance)
Aleksander Fabijan
 
PPTX
Problem solving with python programming OOP's Concept
rohitsharma24121
 
PPTX
Chapter 07 inheritance
Praveen M Jigajinni
 
PDF
Introduction to OOP in python inheritance
Aleksander Fabijan
 
PPTX
Object oriented Programming concepts explained(3).pptx
grad25iconinfocus
 
PDF
Unit_3_2_INHERITANUnit_3_2_INHERITANCE.pdfCE.pdf
RutviBaraiya
 
PPTX
PYTHON OBJECT-ORIENTED PROGRAMMING.pptx
hpearl130
 
PPTX
OOP-part-2 object oriented programming.pptx
palmakyonna
 
PDF
Unit 3-Classes ,Objects and Inheritance.pdf
Harsha Patil
 
PPTX
Introduction to OOP_Python_Lecture1.pptx
cpics
 
PPTX
Dps_Python1_Ectracted_2345633_Pptx4.pptx
ecomwithfaith
 
PPT
inheritance in python with full detail.ppt
ssuser7b0a4d
 
PPTX
oops-in-python-240412044200-2d5c6552.pptx
anilvarsha1
 
PPTX
OOPS.pptx
NitinSharma134320
 
Inheritance
JayanthiNeelampalli
 
arthimetic operator,classes,objects,instant
ssuser77162c
 
python note.pdf
Nagendra504676
 
Inheritance_Polymorphism_Overloading_overriding.pptx
MalligaarjunanN
 
Class and Objects in python programming.pptx
Rajtherock
 
Python oop - class 2 (inheritance)
Aleksander Fabijan
 
Problem solving with python programming OOP's Concept
rohitsharma24121
 
Chapter 07 inheritance
Praveen M Jigajinni
 
Introduction to OOP in python inheritance
Aleksander Fabijan
 
Object oriented Programming concepts explained(3).pptx
grad25iconinfocus
 
Unit_3_2_INHERITANUnit_3_2_INHERITANCE.pdfCE.pdf
RutviBaraiya
 
PYTHON OBJECT-ORIENTED PROGRAMMING.pptx
hpearl130
 
OOP-part-2 object oriented programming.pptx
palmakyonna
 
Unit 3-Classes ,Objects and Inheritance.pdf
Harsha Patil
 
Introduction to OOP_Python_Lecture1.pptx
cpics
 
Dps_Python1_Ectracted_2345633_Pptx4.pptx
ecomwithfaith
 
inheritance in python with full detail.ppt
ssuser7b0a4d
 
oops-in-python-240412044200-2d5c6552.pptx
anilvarsha1
 

More from abigailjudith8 (12)

PPTX
Endpoint Security - - IP layer Attacks and Vulnerabilities
abigailjudith8
 
PPTX
Endpoint Security - Network Security Infrastructure
abigailjudith8
 
PPTX
Polymorphism_in_Python_Programming_Language
abigailjudith8
 
PPTX
Encapsulation_Python_Programming_Language
abigailjudith8
 
PPTX
Application and Data security and Privacy.pptx
abigailjudith8
 
PPTX
Cyber Hackathon Media Campaign Proposal (1).pptx
abigailjudith8
 
PPTX
SVM FOR GRADE 11 pearson Btec 3rd level.ppt
abigailjudith8
 
PPTX
MACHINE LEARNING INTRODUCTION FOR BEGINNERS
abigailjudith8
 
PPT
lect1-introductiontoprogramminglanguages-130130013038-phpapp02.ppt
abigailjudith8
 
PPTX
SVM introduction for machine learning engineers
abigailjudith8
 
PPTX
Big Data for Pearson Btec Higher level 3.ppt
abigailjudith8
 
PPTX
INTRODUCTION TO PROGRAMMING and Python.pptx
abigailjudith8
 
Endpoint Security - - IP layer Attacks and Vulnerabilities
abigailjudith8
 
Endpoint Security - Network Security Infrastructure
abigailjudith8
 
Polymorphism_in_Python_Programming_Language
abigailjudith8
 
Encapsulation_Python_Programming_Language
abigailjudith8
 
Application and Data security and Privacy.pptx
abigailjudith8
 
Cyber Hackathon Media Campaign Proposal (1).pptx
abigailjudith8
 
SVM FOR GRADE 11 pearson Btec 3rd level.ppt
abigailjudith8
 
MACHINE LEARNING INTRODUCTION FOR BEGINNERS
abigailjudith8
 
lect1-introductiontoprogramminglanguages-130130013038-phpapp02.ppt
abigailjudith8
 
SVM introduction for machine learning engineers
abigailjudith8
 
Big Data for Pearson Btec Higher level 3.ppt
abigailjudith8
 
INTRODUCTION TO PROGRAMMING and Python.pptx
abigailjudith8
 
Ad

Recently uploaded (20)

PDF
Hyderabad MuleSoft In-Person Meetup (June 21, 2025) Slides
Ravi Tamada
 
PDF
Plugging AI into everything: Model Context Protocol Simplified.pdf
Abati Adewale
 
PDF
Unlocking FME Flow’s Potential: Architecture Design for Modern Enterprises
Safe Software
 
PDF
Pipeline Industry IoT - Real Time Data Monitoring
Safe Software
 
PPTX
Smarter Governance with AI: What Every Board Needs to Know
OnBoard
 
PDF
5 Things to Consider When Deploying AI in Your Enterprise
Safe Software
 
PPTX
Reimaginando la Ciberdefensa: De Copilots a Redes de Agentes
Cristian Garcia G.
 
PDF
Understanding The True Cost of DynamoDB Webinar
ScyllaDB
 
PDF
Proactive Server and System Monitoring with FME: Using HTTP and System Caller...
Safe Software
 
PPTX
Mastering Authorization: Integrating Authentication and Authorization Data in...
Hitachi, Ltd. OSS Solution Center.
 
PDF
My Journey from CAD to BIM: A True Underdog Story
Safe Software
 
PPTX
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Pitch ...
Michele Kryston
 
PPTX
Enabling the Digital Artisan – keynote at ICOCI 2025
Alan Dix
 
PDF
ArcGIS Utility Network Migration - The Hunter Water Story
Safe Software
 
PDF
Next level data operations using Power Automate magic
Andries den Haan
 
PPTX
The birth and death of Stars - earth and life science
rizellemarieastrolo
 
PDF
Quantum AI Discoveries: Fractal Patterns Consciousness and Cyclical Universes
Saikat Basu
 
PDF
How to Visualize the ​Spatio-Temporal Data Using CesiumJS​
SANGHEE SHIN
 
PDF
LLM Search Readiness Audit - Dentsu x SEO Square - June 2025.pdf
Nick Samuel
 
PDF
“Scaling i.MX Applications Processors’ Native Edge AI with Discrete AI Accele...
Edge AI and Vision Alliance
 
Hyderabad MuleSoft In-Person Meetup (June 21, 2025) Slides
Ravi Tamada
 
Plugging AI into everything: Model Context Protocol Simplified.pdf
Abati Adewale
 
Unlocking FME Flow’s Potential: Architecture Design for Modern Enterprises
Safe Software
 
Pipeline Industry IoT - Real Time Data Monitoring
Safe Software
 
Smarter Governance with AI: What Every Board Needs to Know
OnBoard
 
5 Things to Consider When Deploying AI in Your Enterprise
Safe Software
 
Reimaginando la Ciberdefensa: De Copilots a Redes de Agentes
Cristian Garcia G.
 
Understanding The True Cost of DynamoDB Webinar
ScyllaDB
 
Proactive Server and System Monitoring with FME: Using HTTP and System Caller...
Safe Software
 
Mastering Authorization: Integrating Authentication and Authorization Data in...
Hitachi, Ltd. OSS Solution Center.
 
My Journey from CAD to BIM: A True Underdog Story
Safe Software
 
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Pitch ...
Michele Kryston
 
Enabling the Digital Artisan – keynote at ICOCI 2025
Alan Dix
 
ArcGIS Utility Network Migration - The Hunter Water Story
Safe Software
 
Next level data operations using Power Automate magic
Andries den Haan
 
The birth and death of Stars - earth and life science
rizellemarieastrolo
 
Quantum AI Discoveries: Fractal Patterns Consciousness and Cyclical Universes
Saikat Basu
 
How to Visualize the ​Spatio-Temporal Data Using CesiumJS​
SANGHEE SHIN
 
LLM Search Readiness Audit - Dentsu x SEO Square - June 2025.pdf
Nick Samuel
 
“Scaling i.MX Applications Processors’ Native Edge AI with Discrete AI Accele...
Edge AI and Vision Alliance
 
Ad

Inheritance_in_OOP_using Python Programming

  • 1. Inheritance in Object-Oriented Programming (OOP) Understanding Classes, Objects, and Inheritance in Python Abigail Judith Date
  • 2. Introduction to OOP and Inheritance Object-Oriented Programming (OOP): - Focuses on objects that interact with each other. - Concepts include classes, objects, inheritance, polymorphism, encapsulation, and abstraction. Inheritance: - Inheritance is the mechanism that allows one class (child) to inherit attributes and methods from another class (parent). - Benefits of inheritance: Reusability, Extensibility, Improved code organization.
  • 3. Why Use Inheritance? - Reusability: Child classes can reuse code from the parent class, avoiding duplication. - Extensibility: Child classes can extend the functionality of parent classes. - Simplifies Maintenance: Changes to the parent class automatically reflect in the child classes.
  • 4. Types of Inheritance - Single Inheritance: A child class inherits from one parent class. Example: class Dog(Animal): - Multiple Inheritance: A child class inherits from more than one parent class. Example: class Dog(Mammal, Animal): - Multilevel Inheritance: A class inherits from a class, which is itself derived from another class. Example: class Puppy(Dog):
  • 5. Inheritance Syntax Basic Syntax: class ParentClass: def __init__(self): pass def method_in_parent(self): pass class ChildClass(ParentClass): # Inherits from ParentClass def __init__(self): super().__init__() # Calls the constructor of ParentClass pass def method_in_child(self): pass
  • 6. Creating Objects for the Class Class Object Instantiation: When a class is defined, you can create objects (instances) of the class to access its methods and attributes. Example: class Animal: def __init__(self, name): self.name = name # Creating an object of Animal class animal = Animal("Lion") print(animal.name) # Output: Lion
  • 7. Demonstrating Inheritance with Example Parent Class: class Animal: def __init__(self, name): self.name = name def speak(self): print(f"{self.name} makes a sound") Child Class: class Dog(Animal): def __init__(self, name, breed): super().__init__(name) self.breed = breed def speak(self): print(f"{self.name} barks") Creating Objects for the Child Class: dog = Dog("Buddy", "Golden Retriever") dog.speak() # Output: Buddy barks
  • 8. # Parent class class Parent: def __init__(self): self.parent_var = "I am a Parent variable" def parent_method(self): print("This is a method from Parent class") # First Child class class Child1(Parent): def child1_method(self): print("This is a method from Child1 class") # Second Child class class Child2(Parent): def child2_method(self): print("This is a method from Child2 class") # Creating objects of child classes obj1 = Child1() obj2 = Child2() # Accessing Parent class properties and methods print(obj1.parent_var) # Accessing Parent's variable obj1.parent_method() # Calling
  • 9. class Animal: def move(self): print('Animals can move') class Bird: def fly(self): print('Birds can fly') class Bat(Animal, Bird): def sound(self): print('Bats make high-pitched sounds') bat = Bat() bat.move() bat.fly() bat.sound()
  • 10. Python follows MRO when resolving methods. - The `__mro__` attribute shows the method lookup order. - Example: print(Bat.__mro__) Output: (<class 'Bat'>, <class 'Animal'>, <class 'Bird'>, <class 'object'>)
  • 11. Method Overriding in Inheritance Definition: The child class can provide its own implementation of a method that is already defined in the parent class. Example: class Animal: def speak(self): print("Animal makes a sound") class Dog(Animal): def speak(self): print("Dog barks") animal = Animal() animal.speak() # Output: Animal makes a sound dog = Dog() dog.speak() # Output: Dog barks
  • 12. Super Function and Inheritance The super() function is used to call methods from the parent class. This is particularly useful in the child class constructor to call the parent class's __init__() method. Example: class Animal: def __init__(self, name): self.name = name def speak(self): print(f"{self.name} makes a sound") class Dog(Animal): def __init__(self, name, breed): super().__init__(name) # Call the parent class constructor self.breed = breed def speak(self): print(f"{self.name} barks") dog = Dog("Buddy", "Golden Retriever") dog.speak() # Output: Buddy barks
  • 13. Private and Protected Attributes in Inheritance Private Attributes: - Private attributes (using __) are not accessible outside the class, even in subclasses. Example: class Animal: def __init__(self, name): self.__name = name # Private attribute def get_name(self): return self.__name class Dog(Animal): def __init__(self, name): super().__init__(name) dog = Dog("Buddy") print(dog.get_name()) # Access private attribute via public method
  • 14. Access Modifiers in Inheritance • Public Attributes: Accessible from anywhere. • Protected Attributes: Indicated by a single underscore (_), meant for internal use within a class or subclass. • Private Attributes: Indicated by double underscores (__), not directly accessible outside the class or subclass.
  • 15. Inheritance in Action - Example Parent Class: class Animal: def __init__(self, name): self.name = name def speak(self): print(f"{self.name} makes a sound") Child Class: class Dog(Animal): def speak(self): print(f"{self.name} barks") class Cat(Animal): def speak(self): print(f"{self.name} meows") dog = Dog("Buddy") cat = Cat("Kitty") dog.speak() # Buddy barks cat.speak() # Kitty meows
  • 16. Conclusion - Inheritance allows one class to acquire attributes and methods from another. - It promotes code reuse and provides a way to extend or modify functionality. - You can override methods in the child class and use super() to call the parent class's methods.
  • 17. Q&A • Open floor for questions and clarifications.