Monday, October 9, 2023

Inheritance in Python (Theory)

  • Inheritance : It allows us to define a class that inherits all the methods and properties from another class.

  • Parent class /Base Class : Which methods and properties being inherited
  • Child Class / Derived Class : Which inherits properites and method of other class

  • Any class can be a Parent Class
  • While defining a child class, we just need to pass the name of parent class as an argument

# __init__() Function and child class : If we define __init__() function in child class then it will overide the functionality of inherited __init__() function of parent class.

# __init__() this function call automatically on object creation of child class.

# Note: we can explicity call parent class __init__() function inside the definition of child class __init__() funciton as follow :

ParentClassName.__init__()

Types of Inheritance in Python: Single Inheritance

Multiple Inheritance

Multilevel Inheritance

Hierarchical Inheritance

Hybrid Inheritance

Use the super() Function in Child Class

# super() function is used to access any property and method of parent class

# We can add new properties and method in child class as its own

No comments:

Post a Comment