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)

Lecture on Python OP concepts of Polymorpysim and Inheritance.pdf
Lecture on Python OP concepts of Polymorpysim and Inheritance.pdfLecture on Python OP concepts of Polymorpysim and Inheritance.pdf
Lecture on Python OP concepts of Polymorpysim and Inheritance.pdf
waqaskhan428678
 
Inheritance & Polymorphism
Inheritance & PolymorphismInheritance & Polymorphism
Inheritance & Polymorphism
SAGARDAVE29
 
Object_Oriented_Programming_Unit3.pdf
Object_Oriented_Programming_Unit3.pdfObject_Oriented_Programming_Unit3.pdf
Object_Oriented_Programming_Unit3.pdf
Koteswari Kasireddy
 
Python Lecture 13
Python Lecture 13Python Lecture 13
Python Lecture 13
Inzamam Baig
 
ACM init() Day 6
ACM init() Day 6ACM init() Day 6
ACM init() Day 6
UCLA Association of Computing Machinery
 
Object-oriented Programming-with C#
Object-oriented Programming-with C#Object-oriented Programming-with C#
Object-oriented Programming-with C#
Doncho Minkov
 
Inheritance and polymorphism
Inheritance and polymorphism   Inheritance and polymorphism
Inheritance and polymorphism
baabtra.com - No. 1 supplier of quality freshers
 
Chapter 07 inheritance
Chapter 07 inheritanceChapter 07 inheritance
Chapter 07 inheritance
Praveen M Jigajinni
 
Metaprogramming 101
Metaprogramming 101Metaprogramming 101
Metaprogramming 101
Nando Vieira
 
Python Programming - VIII. Inheritance and Polymorphism
Python Programming - VIII. Inheritance and PolymorphismPython Programming - VIII. Inheritance and Polymorphism
Python Programming - VIII. Inheritance and Polymorphism
Ranel Padon
 
INHERITANCE ppt of python.pptx & PYTHON INHERITANCE PPT
INHERITANCE ppt of python.pptx & PYTHON INHERITANCE PPTINHERITANCE ppt of python.pptx & PYTHON INHERITANCE PPT
INHERITANCE ppt of python.pptx & PYTHON INHERITANCE PPT
deepuranjankumar08
 
Inheritance and polymorphism oops concepts in python
Inheritance and polymorphism  oops concepts in pythonInheritance and polymorphism  oops concepts in python
Inheritance and polymorphism oops concepts in python
deepalishinkar1
 
Class and Objects in PHP
Class and Objects in PHPClass and Objects in PHP
Class and Objects in PHP
Ramasubbu .P
 
10 classes
10 classes10 classes
10 classes
Naomi Boyoro
 
Oops implemetation material
Oops implemetation materialOops implemetation material
Oops implemetation material
Deepak Solanki
 
PHP-05-Objects.ppt
PHP-05-Objects.pptPHP-05-Objects.ppt
PHP-05-Objects.ppt
rani marri
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
Paumil Patel
 
inheritance-16031525566nbhij56604452.pdf
inheritance-16031525566nbhij56604452.pdfinheritance-16031525566nbhij56604452.pdf
inheritance-16031525566nbhij56604452.pdf
kashafishfaq21
 
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYAPYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
Maulik Borsaniya
 
Lec5.ppt
Lec5.pptLec5.ppt
Lec5.ppt
CharlesAsiedu4
 
Lecture on Python OP concepts of Polymorpysim and Inheritance.pdf
Lecture on Python OP concepts of Polymorpysim and Inheritance.pdfLecture on Python OP concepts of Polymorpysim and Inheritance.pdf
Lecture on Python OP concepts of Polymorpysim and Inheritance.pdf
waqaskhan428678
 
Inheritance & Polymorphism
Inheritance & PolymorphismInheritance & Polymorphism
Inheritance & Polymorphism
SAGARDAVE29
 
Object_Oriented_Programming_Unit3.pdf
Object_Oriented_Programming_Unit3.pdfObject_Oriented_Programming_Unit3.pdf
Object_Oriented_Programming_Unit3.pdf
Koteswari Kasireddy
 
Object-oriented Programming-with C#
Object-oriented Programming-with C#Object-oriented Programming-with C#
Object-oriented Programming-with C#
Doncho Minkov
 
Metaprogramming 101
Metaprogramming 101Metaprogramming 101
Metaprogramming 101
Nando Vieira
 
Python Programming - VIII. Inheritance and Polymorphism
Python Programming - VIII. Inheritance and PolymorphismPython Programming - VIII. Inheritance and Polymorphism
Python Programming - VIII. Inheritance and Polymorphism
Ranel Padon
 
INHERITANCE ppt of python.pptx & PYTHON INHERITANCE PPT
INHERITANCE ppt of python.pptx & PYTHON INHERITANCE PPTINHERITANCE ppt of python.pptx & PYTHON INHERITANCE PPT
INHERITANCE ppt of python.pptx & PYTHON INHERITANCE PPT
deepuranjankumar08
 
Inheritance and polymorphism oops concepts in python
Inheritance and polymorphism  oops concepts in pythonInheritance and polymorphism  oops concepts in python
Inheritance and polymorphism oops concepts in python
deepalishinkar1
 
Class and Objects in PHP
Class and Objects in PHPClass and Objects in PHP
Class and Objects in PHP
Ramasubbu .P
 
Oops implemetation material
Oops implemetation materialOops implemetation material
Oops implemetation material
Deepak Solanki
 
PHP-05-Objects.ppt
PHP-05-Objects.pptPHP-05-Objects.ppt
PHP-05-Objects.ppt
rani marri
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
Paumil Patel
 
inheritance-16031525566nbhij56604452.pdf
inheritance-16031525566nbhij56604452.pdfinheritance-16031525566nbhij56604452.pdf
inheritance-16031525566nbhij56604452.pdf
kashafishfaq21
 
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYAPYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
Maulik Borsaniya
 

More from abigailjudith8 (12)

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

Recently uploaded (20)

Edge-banding-machines-edgeteq-s-200-en-.pdf
Edge-banding-machines-edgeteq-s-200-en-.pdfEdge-banding-machines-edgeteq-s-200-en-.pdf
Edge-banding-machines-edgeteq-s-200-en-.pdf
AmirStern2
 
Your startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean accountYour startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean account
angelo60207
 
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Safe Software
 
The State of Web3 Industry- Industry Report
The State of Web3 Industry- Industry ReportThe State of Web3 Industry- Industry Report
The State of Web3 Industry- Industry Report
Liveplex
 
Murdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementaryMurdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementary
JorgeSemperteguiMont
 
Oracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI ProfessionalOracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI Professional
VICTOR MAESTRE RAMIREZ
 
Establish Visibility and Manage Risk in the Supply Chain with Anchore SBOM
Establish Visibility and Manage Risk in the Supply Chain with Anchore SBOMEstablish Visibility and Manage Risk in the Supply Chain with Anchore SBOM
Establish Visibility and Manage Risk in the Supply Chain with Anchore SBOM
Anchore
 
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdfcnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
AmirStern2
 
Ben Blair - Operating Safely in a Vibe Coding World
Ben Blair - Operating Safely in a Vibe Coding WorldBen Blair - Operating Safely in a Vibe Coding World
Ben Blair - Operating Safely in a Vibe Coding World
AWS Chicago
 
Oracle Cloud and AI Specialization Program
Oracle Cloud and AI Specialization ProgramOracle Cloud and AI Specialization Program
Oracle Cloud and AI Specialization Program
VICTOR MAESTRE RAMIREZ
 
Crypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdfCrypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdf
Stephen Perrenod
 
Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...
Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...
Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...
Precisely
 
Oracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI FoundationsOracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI Foundations
VICTOR MAESTRE RAMIREZ
 
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven InfrastructureNo-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
Safe Software
 
Cisco ISE Performance, Scalability and Best Practices.pdf
Cisco ISE Performance, Scalability and Best Practices.pdfCisco ISE Performance, Scalability and Best Practices.pdf
Cisco ISE Performance, Scalability and Best Practices.pdf
superdpz
 
Domino IQ – What to Expect, First Steps and Use Cases
Domino IQ – What to Expect, First Steps and Use CasesDomino IQ – What to Expect, First Steps and Use Cases
Domino IQ – What to Expect, First Steps and Use Cases
panagenda
 
If You Use Databricks, You Definitely Need FME
If You Use Databricks, You Definitely Need FMEIf You Use Databricks, You Definitely Need FME
If You Use Databricks, You Definitely Need FME
Safe Software
 
Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025
Safe Software
 
How Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdf
How Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdfHow Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdf
How Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdf
Rejig Digital
 
Enabling BIM / GIS integrations with Other Systems with FME
Enabling BIM / GIS integrations with Other Systems with FMEEnabling BIM / GIS integrations with Other Systems with FME
Enabling BIM / GIS integrations with Other Systems with FME
Safe Software
 
Edge-banding-machines-edgeteq-s-200-en-.pdf
Edge-banding-machines-edgeteq-s-200-en-.pdfEdge-banding-machines-edgeteq-s-200-en-.pdf
Edge-banding-machines-edgeteq-s-200-en-.pdf
AmirStern2
 
Your startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean accountYour startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean account
angelo60207
 
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Safe Software
 
The State of Web3 Industry- Industry Report
The State of Web3 Industry- Industry ReportThe State of Web3 Industry- Industry Report
The State of Web3 Industry- Industry Report
Liveplex
 
Murdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementaryMurdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementary
JorgeSemperteguiMont
 
Oracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI ProfessionalOracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI Professional
VICTOR MAESTRE RAMIREZ
 
Establish Visibility and Manage Risk in the Supply Chain with Anchore SBOM
Establish Visibility and Manage Risk in the Supply Chain with Anchore SBOMEstablish Visibility and Manage Risk in the Supply Chain with Anchore SBOM
Establish Visibility and Manage Risk in the Supply Chain with Anchore SBOM
Anchore
 
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdfcnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
AmirStern2
 
Ben Blair - Operating Safely in a Vibe Coding World
Ben Blair - Operating Safely in a Vibe Coding WorldBen Blair - Operating Safely in a Vibe Coding World
Ben Blair - Operating Safely in a Vibe Coding World
AWS Chicago
 
Oracle Cloud and AI Specialization Program
Oracle Cloud and AI Specialization ProgramOracle Cloud and AI Specialization Program
Oracle Cloud and AI Specialization Program
VICTOR MAESTRE RAMIREZ
 
Crypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdfCrypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdf
Stephen Perrenod
 
Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...
Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...
Precisely Demo Showcase: Powering ServiceNow Discovery with Precisely Ironstr...
Precisely
 
Oracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI FoundationsOracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI Foundations
VICTOR MAESTRE RAMIREZ
 
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven InfrastructureNo-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
Safe Software
 
Cisco ISE Performance, Scalability and Best Practices.pdf
Cisco ISE Performance, Scalability and Best Practices.pdfCisco ISE Performance, Scalability and Best Practices.pdf
Cisco ISE Performance, Scalability and Best Practices.pdf
superdpz
 
Domino IQ – What to Expect, First Steps and Use Cases
Domino IQ – What to Expect, First Steps and Use CasesDomino IQ – What to Expect, First Steps and Use Cases
Domino IQ – What to Expect, First Steps and Use Cases
panagenda
 
If You Use Databricks, You Definitely Need FME
If You Use Databricks, You Definitely Need FMEIf You Use Databricks, You Definitely Need FME
If You Use Databricks, You Definitely Need FME
Safe Software
 
Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025
Safe Software
 
How Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdf
How Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdfHow Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdf
How Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdf
Rejig Digital
 
Enabling BIM / GIS integrations with Other Systems with FME
Enabling BIM / GIS integrations with Other Systems with FMEEnabling BIM / GIS integrations with Other Systems with FME
Enabling BIM / GIS integrations with Other Systems with FME
Safe Software
 
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.