SlideShare a Scribd company logo
Object-Oriented Programming (OOP)
What is Inheritance?
• Definition:
 Inheritance is a fundamental concept in Object-
Oriented Programming (OOP) that allows one
class (the child class) to inherit the properties
and behaviors (methods) of another class (the
parent class).
• Purpose:
 The primary goal of inheritance is to promote
code reusability, reduce redundancy, and
establish a hierarchical relationship between
classes. It allows for a more organized and
maintainable code structure.
• Analogy:
 Consider inheritance as a family tree, where
traits and behaviors are passed down from
parents to children.
Parent Class
• Definition:
 The parent class, also known as the base class or super class, serves as a
blueprint for other classes. It contains attributes and methods that are
common to all derived classes.
• Characteristics:
 Attributes: Variables that hold data related to the class.
 Methods: Functions that define behaviors and actions related to the class.
• Example:
• Class Name: Animal
 Attributes: species, age
 Methods: make_sound()
• class Animal:
 def __init__(self, species, age):
 self.species = species
 self.age = age
• def make_sound(self):
 return "Some sound"
Child Class
• Definition:
• The child class, also known as the derived class or sub class,
inherits properties and methods from the parent class.
• It can also have additional attributes and methods or override
existing ones.
• Characteristics:
• Inheritance: Inherits attributes and methods from the parent
class.
• Overriding: Can redefine methods to provide specific
implementations.
• Example:
• Class Name: Dog (inherits from Animal)
• Additional Attribute: breed
• Overridden Method: make_sound() to return "Bark!“
• class Dog(Animal):
 def __init__(self, species, age, breed):
 super().__init__(species, age)
 self.breed = breed
• def make_sound(self):
 return "Bark!"
Overriding Parent Methods
• Definition:
• Overriding occurs when a child class provides a
specific implementation of a method that is already
defined in its parent class.
• This allows the child class to tailor the behavior of
the inherited method to fit its needs.
• Purpose:
• It enables polymorphism, allowing different classes
to have methods with the same name but different
behaviors.
• Example:
• In the Dog class, the make_sound() method is
overridden to return "Bark!" instead of the generic
"Some sound" from the Animal class.
The super() Function
• Definition:
• The super() function is used in a child class to call
methods from its parent class. This is particularly
useful for invoking the parent class’s constructor or
methods.
• Benefits:
• Simplifies the process of calling parent class
methods.
• Helps avoid explicitly naming the parent class,
making the code more maintainable.
• Usage in the Example:
• In the Dog class constructor,
super().__init__(species, age) initializes the
attributes defined in the parent class Animal.
Types of Inheritance
• Overview:
• There are several types of inheritance
in OOP, which dictate how classes can
inherit from one another.
Understanding these types is crucial
for designing class hierarchies
effectively.
• Key Points:
• Inheritance establishes a "is-a"
relationship between classes.
• It promotes code reuse and reduces
redundancy.
• Proper use of inheritance can simplify
code and improve maintainability.
• Consider potential complexity and
ambiguity, especially with multiple
inheritance.
Single Inheritance
• Definition:
• Single inheritance occurs when a child class
inherits from one parent class only.
• This is the simplest form of inheritance and is
widely used in OOP.
• Characteristics:
 Easy to implement and understand.
 Reduces complexity in the class hierarchy.
• Example:
• Parent Class: Animal
• Child Class: Dog
• class Dog(Animal):
 # Inherits attributes and methods from Animal
 pass
Multiple Inheritance
• Definition:
• Multiple inheritance allows a child class to inherit from multiple parent classes.
This can lead to more complex class hierarchies and can introduce ambiguity if not
managed properly.
• Characteristics:
• Can promote greater flexibility in class design.
• Potential for conflicts, particularly with method names (the "diamond problem").
• Example:
• Parent Classes: Dog and HelperAnimal
• Child Class: GuideDog
• class HelperAnimal:
• def assist(self):
• return "Assisting human"
• class GuideDog(Dog, HelperAnimal):
• def guide(self):
• return "Guiding the visually impaired"
Hierarchical Inheritance
• Definition:
• Hierarchical inheritance occurs when multiple child classes inherit from
• a single parent class.
• Characteristics:
• Allows for shared attributes and methods from a common parent class.
• Promotes code reusability.
• Different child classes can have additional, unique functionality.
• Example:
• Parent Class: Animal
• Child Classes: Dog, Cat
• class Animal:
 def make_sound(self):
 return "Some sound"
• class Dog(Animal):
 def bark(self):
 return "Woof!"
• class Cat(Animal):
 def meow(self):
 return "Meow!"
Multilevel Inheritance
• Definition:
• Multilevel inheritance occurs when a child class inherits from another
child class, creating a chain of inheritance.
• Characteristics:
• A derived class becomes the base class for another class.
• Enables a deeper inheritance hierarchy.
• Example:
• Grandparent Class: Animal
• Parent Class: Dog
• Child Class: GuideDog
• class Animal:
 def make_sound(self):
 return "Some sound"
• class Dog(Animal):
 def bark(self):
 return "Woof!"
• class GuideDog(Dog):
 def guide(self):
 return "Guiding the visually impaired"
Hybrid Inheritance
• Definition:
• Hybrid inheritance is a combination of two or more types of
inheritance, such as single, multiple, multilevel, or hierarchical
inheritance.
• Characteristics:
• Allows for a flexible and more complex class design.
• Example:
 Combining multilevel and multiple inheritance:
• class Animal:
 def make_sound(self):
 return "Some sound"
• class Dog(Animal):
 def bark(self):
 return "Woof!"
• class HelperAnimal:
 def assist(self):
 return "Assisting human"
• class GuideDog(Dog, HelperAnimal):
 def guide(self):

More Related Content

PPTX
Object Oriented Programming -Single Inheritance.pptx
PPTX
Pi j3.1 inheritance
PDF
OOP Inheritance
PPTX
Object Oriented Programming - Copy.pptxb
PDF
Java programming -Object-Oriented Thinking- Inheritance
PPTX
Parent and Child Classes in Object Oriented Programming.pptx
PDF
Unit 3-Classes ,Objects and Inheritance.pdf
PDF
‏‏‏‏‏‏oop lecture objectives will come.pdf
Object Oriented Programming -Single Inheritance.pptx
Pi j3.1 inheritance
OOP Inheritance
Object Oriented Programming - Copy.pptxb
Java programming -Object-Oriented Thinking- Inheritance
Parent and Child Classes in Object Oriented Programming.pptx
Unit 3-Classes ,Objects and Inheritance.pdf
‏‏‏‏‏‏oop lecture objectives will come.pdf

Similar to Lecture on Python OP concepts of Polymorpysim and Inheritance.pdf (20)

PPTX
Ayan Das_25300121057.pptx
PPTX
Polymorphism
PPTX
Classes_and_Objects_in_Pythonoopsconcept.pptx
PPTX
Inheritance in Object-Oriented Programming (OOP) (not).pptx
PPTX
Inheritance in Java is a mechanism in which one object acquires all the prope...
PPT
Inheritance in java
PPTX
OOPS (Object Oriented Programming System) CONCEPTS
PPTX
inheritance.pptxfdfgfgfgfgdfgfgdfgdfgerre
PPTX
Object oriented programming
PPTX
Inheritance & Polymorphism
PDF
How to Implement Hierarchical Inheritance in Java: A Step-by-Step Guide
PPTX
arthimetic operator,classes,objects,instant
PPTX
Inheritance in java.pptx_20241025_101324_0000.pptx.pptx
PPTX
Java chapter 5
PPTX
PPTX
29csharp
PPTX
[OOP - Lec 04,05] Basic Building Blocks of OOP
PPTX
OO relationships between classes
PPTX
Java interfaces
Ayan Das_25300121057.pptx
Polymorphism
Classes_and_Objects_in_Pythonoopsconcept.pptx
Inheritance in Object-Oriented Programming (OOP) (not).pptx
Inheritance in Java is a mechanism in which one object acquires all the prope...
Inheritance in java
OOPS (Object Oriented Programming System) CONCEPTS
inheritance.pptxfdfgfgfgfgdfgfgdfgdfgerre
Object oriented programming
Inheritance & Polymorphism
How to Implement Hierarchical Inheritance in Java: A Step-by-Step Guide
arthimetic operator,classes,objects,instant
Inheritance in java.pptx_20241025_101324_0000.pptx.pptx
Java chapter 5
29csharp
[OOP - Lec 04,05] Basic Building Blocks of OOP
OO relationships between classes
Java interfaces
Ad

Recently uploaded (20)

PPT
Quality review (1)_presentation of this 21
PDF
22.Patil - Early prediction of Alzheimer’s disease using convolutional neural...
PDF
BF and FI - Blockchain, fintech and Financial Innovation Lesson 2.pdf
PDF
.pdf is not working space design for the following data for the following dat...
PPTX
Business Ppt On Nestle.pptx huunnnhhgfvu
PPTX
Qualitative Qantitative and Mixed Methods.pptx
PPTX
Introduction to Basics of Ethical Hacking and Penetration Testing -Unit No. 1...
PDF
Recruitment and Placement PPT.pdfbjfibjdfbjfobj
PDF
“Getting Started with Data Analytics Using R – Concepts, Tools & Case Studies”
PDF
Lecture1 pattern recognition............
PDF
Fluorescence-microscope_Botany_detailed content
PDF
annual-report-2024-2025 original latest.
PPTX
01_intro xxxxxxxxxxfffffffffffaaaaaaaaaaafg
PPTX
Introduction to Knowledge Engineering Part 1
PPTX
IBA_Chapter_11_Slides_Final_Accessible.pptx
PPTX
Computer network topology notes for revision
PPTX
MODULE 8 - DISASTER risk PREPAREDNESS.pptx
PPTX
1_Introduction to advance data techniques.pptx
PPTX
advance b rammar.pptxfdgdfgdfsgdfgsdgfdfgdfgsdfgdfgdfg
PPT
Miokarditis (Inflamasi pada Otot Jantung)
Quality review (1)_presentation of this 21
22.Patil - Early prediction of Alzheimer’s disease using convolutional neural...
BF and FI - Blockchain, fintech and Financial Innovation Lesson 2.pdf
.pdf is not working space design for the following data for the following dat...
Business Ppt On Nestle.pptx huunnnhhgfvu
Qualitative Qantitative and Mixed Methods.pptx
Introduction to Basics of Ethical Hacking and Penetration Testing -Unit No. 1...
Recruitment and Placement PPT.pdfbjfibjdfbjfobj
“Getting Started with Data Analytics Using R – Concepts, Tools & Case Studies”
Lecture1 pattern recognition............
Fluorescence-microscope_Botany_detailed content
annual-report-2024-2025 original latest.
01_intro xxxxxxxxxxfffffffffffaaaaaaaaaaafg
Introduction to Knowledge Engineering Part 1
IBA_Chapter_11_Slides_Final_Accessible.pptx
Computer network topology notes for revision
MODULE 8 - DISASTER risk PREPAREDNESS.pptx
1_Introduction to advance data techniques.pptx
advance b rammar.pptxfdgdfgdfsgdfgsdgfdfgdfgsdfgdfgdfg
Miokarditis (Inflamasi pada Otot Jantung)
Ad

Lecture on Python OP concepts of Polymorpysim and Inheritance.pdf

  • 2. What is Inheritance? • Definition:  Inheritance is a fundamental concept in Object- Oriented Programming (OOP) that allows one class (the child class) to inherit the properties and behaviors (methods) of another class (the parent class). • Purpose:  The primary goal of inheritance is to promote code reusability, reduce redundancy, and establish a hierarchical relationship between classes. It allows for a more organized and maintainable code structure. • Analogy:  Consider inheritance as a family tree, where traits and behaviors are passed down from parents to children.
  • 3. Parent Class • Definition:  The parent class, also known as the base class or super class, serves as a blueprint for other classes. It contains attributes and methods that are common to all derived classes. • Characteristics:  Attributes: Variables that hold data related to the class.  Methods: Functions that define behaviors and actions related to the class. • Example: • Class Name: Animal  Attributes: species, age  Methods: make_sound() • class Animal:  def __init__(self, species, age):  self.species = species  self.age = age • def make_sound(self):  return "Some sound"
  • 4. Child Class • Definition: • The child class, also known as the derived class or sub class, inherits properties and methods from the parent class. • It can also have additional attributes and methods or override existing ones. • Characteristics: • Inheritance: Inherits attributes and methods from the parent class. • Overriding: Can redefine methods to provide specific implementations. • Example: • Class Name: Dog (inherits from Animal) • Additional Attribute: breed • Overridden Method: make_sound() to return "Bark!“ • class Dog(Animal):  def __init__(self, species, age, breed):  super().__init__(species, age)  self.breed = breed • def make_sound(self):  return "Bark!"
  • 5. Overriding Parent Methods • Definition: • Overriding occurs when a child class provides a specific implementation of a method that is already defined in its parent class. • This allows the child class to tailor the behavior of the inherited method to fit its needs. • Purpose: • It enables polymorphism, allowing different classes to have methods with the same name but different behaviors. • Example: • In the Dog class, the make_sound() method is overridden to return "Bark!" instead of the generic "Some sound" from the Animal class.
  • 6. The super() Function • Definition: • The super() function is used in a child class to call methods from its parent class. This is particularly useful for invoking the parent class’s constructor or methods. • Benefits: • Simplifies the process of calling parent class methods. • Helps avoid explicitly naming the parent class, making the code more maintainable. • Usage in the Example: • In the Dog class constructor, super().__init__(species, age) initializes the attributes defined in the parent class Animal.
  • 7. Types of Inheritance • Overview: • There are several types of inheritance in OOP, which dictate how classes can inherit from one another. Understanding these types is crucial for designing class hierarchies effectively. • Key Points: • Inheritance establishes a "is-a" relationship between classes. • It promotes code reuse and reduces redundancy. • Proper use of inheritance can simplify code and improve maintainability. • Consider potential complexity and ambiguity, especially with multiple inheritance.
  • 8. Single Inheritance • Definition: • Single inheritance occurs when a child class inherits from one parent class only. • This is the simplest form of inheritance and is widely used in OOP. • Characteristics:  Easy to implement and understand.  Reduces complexity in the class hierarchy. • Example: • Parent Class: Animal • Child Class: Dog • class Dog(Animal):  # Inherits attributes and methods from Animal  pass
  • 9. Multiple Inheritance • Definition: • Multiple inheritance allows a child class to inherit from multiple parent classes. This can lead to more complex class hierarchies and can introduce ambiguity if not managed properly. • Characteristics: • Can promote greater flexibility in class design. • Potential for conflicts, particularly with method names (the "diamond problem"). • Example: • Parent Classes: Dog and HelperAnimal • Child Class: GuideDog • class HelperAnimal: • def assist(self): • return "Assisting human" • class GuideDog(Dog, HelperAnimal): • def guide(self): • return "Guiding the visually impaired"
  • 10. Hierarchical Inheritance • Definition: • Hierarchical inheritance occurs when multiple child classes inherit from • a single parent class. • Characteristics: • Allows for shared attributes and methods from a common parent class. • Promotes code reusability. • Different child classes can have additional, unique functionality. • Example: • Parent Class: Animal • Child Classes: Dog, Cat • class Animal:  def make_sound(self):  return "Some sound" • class Dog(Animal):  def bark(self):  return "Woof!" • class Cat(Animal):  def meow(self):  return "Meow!"
  • 11. Multilevel Inheritance • Definition: • Multilevel inheritance occurs when a child class inherits from another child class, creating a chain of inheritance. • Characteristics: • A derived class becomes the base class for another class. • Enables a deeper inheritance hierarchy. • Example: • Grandparent Class: Animal • Parent Class: Dog • Child Class: GuideDog • class Animal:  def make_sound(self):  return "Some sound" • class Dog(Animal):  def bark(self):  return "Woof!" • class GuideDog(Dog):  def guide(self):  return "Guiding the visually impaired"
  • 12. Hybrid Inheritance • Definition: • Hybrid inheritance is a combination of two or more types of inheritance, such as single, multiple, multilevel, or hierarchical inheritance. • Characteristics: • Allows for a flexible and more complex class design. • Example:  Combining multilevel and multiple inheritance: • class Animal:  def make_sound(self):  return "Some sound" • class Dog(Animal):  def bark(self):  return "Woof!" • class HelperAnimal:  def assist(self):  return "Assisting human" • class GuideDog(Dog, HelperAnimal):  def guide(self):