A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/inheritance-in-python/ below:

Inheritance in Python - GeeksforGeeks

Inheritance in Python

Last Updated : 11 Jul, 2025

Inheritance is a fundamental concept in object-oriented programming (OOP) that allows a class (called a child or derived class) to inherit attributes and methods from another class (called a parent or base class). This promotes code reuse, modularity, and a hierarchical class structure. In this article, we'll explore inheritance in Python.

Basic Example of Inheritance

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

Python
# Parent class
class Animal:
    def __init__(self, name):
        self.name = name  # Initialize the name attribute

    def speak(self):
        pass  # Placeholder method to be overridden by child classes

# Child class inheriting from Animal
class Dog(Animal):
    def speak(self):
        return f"{self.name} barks!"  # Override the speak method

# Creating an instance of Dog
dog = Dog("Buddy")
print(dog.speak())  
Explanation:

Let's understand inheritance in detail:

Syntax for Inheritance

class ParentClass:

# Parent class code here
pass

class ChildClass(ParentClass):

# Child class code here
pass

Explanation of Python Inheritance Syntax
  1. Parent Class:
  2. Child Class:
Creating a Parent Class

In object-oriented programming, a parent class (also known as a base class) defines common attributes and methods that can be inherited by other classes. These attributes and methods serve as the foundation for the child classes. By using inheritance, child classes can access and extend the functionality provided by the parent class.

Here's an example where Person is the parent class:

Python
# A Python program to demonstrate inheritance
class Person(object):
  
  # Constructor
  def __init__(self, name, id):
    self.name = name
    self.id = id

  # To check if this person is an employee
  def Display(self):
    print(self.name, self.id)


# Driver code
emp = Person("Satyam", 102) # An Object of Person
emp.Display()

Explanation:

Creating a Child Class

A child class (also known as a subclass) is a class that inherits properties and methods from its parent class. The child class can also introduce additional attributes and methods, or even override the ones inherited from the parent.

In this case, Emp is the child class that inherits from the Person class:

Python
class Emp(Person):
  
  def Print(self):
    print("Emp class called")
    
Emp_details = Emp("Mayank", 103)

# calling parent class function
Emp_details.Display()

# Calling child class function
Emp_details.Print()

Explanation:

__init__() Function

__init__() function is a constructor method in Python. It initializes the object's state when the object is created. If the child class does not define its own __init__() method, it will automatically inherit the one from the parent class.

In the example above, the __init__() method in the Employee class ensures that both inherited and new attributes are properly initialized.

Python
# Parent Class: Person
class Person:
    def __init__(self, name, idnumber):
        self.name = name
        self.idnumber = idnumber

# Child Class: Employee
class Employee(Person):
    def __init__(self, name, idnumber, salary, post):
        super().__init__(name, idnumber)  # Calls Person's __init__()
        self.salary = salary
        self.post = post

Explanation:

super() Function

super() function is used to call the parent class’s methods. In particular, it is commonly used in the child class’s __init__() method to initialize inherited attributes. This way, the child class can leverage the functionality of the parent class.

Example:

Python
# Parent Class: Person
class Person:
    def __init__(self, name, idnumber):
        self.name = name
        self.idnumber = idnumber

    def display(self):
        print(self.name)
        print(self.idnumber)

# Child Class: Employee
class Employee(Person):
    def __init__(self, name, idnumber, salary, post):
        super().__init__(name, idnumber)  # Using super() to call Person's __init__()
        self.salary = salary
        self.post = post

Explanation:

Add Properties

Once inheritance is established, both the parent and child classes can have their own properties. Properties are attributes that belong to a class and are used to store data.

Example:

Python
# Parent Class: Person
class Person:
    def __init__(self, name, idnumber):
        self.name = name
        self.idnumber = idnumber

    def display(self):
        print(self.name)
        print(self.idnumber)

# Child Class: Employee
class Employee(Person):
    def __init__(self, name, idnumber, salary, post):
        super().__init__(name, idnumber)
        self.salary = salary
        self.post = post

Explanation:

Types of Python Inheritance
  1. Single Inheritance: A child class inherits from one parent class.
  2. Multiple Inheritance: A child class inherits from more than one parent class.
  3. Multilevel Inheritance: A class is derived from a class which is also derived from another class.
  4. Hierarchical Inheritance: Multiple classes inherit from a single parent class.
  5. Hybrid Inheritance: A combination of more than one type of inheritance.

Example:

Python
# 1. Single Inheritance
class Person:
    def __init__(self, name):
        self.name = name

class Employee(Person):  # Employee inherits from Person
    def __init__(self, name, salary):
        super().__init__(name)
        self.salary = salary

# 2. Multiple Inheritance
class Job:
    def __init__(self, salary):
        self.salary = salary

class EmployeePersonJob(Employee, Job):  # Inherits from both Employee and Job
    def __init__(self, name, salary):
        Employee.__init__(self, name, salary)  # Initialize Employee
        Job.__init__(self, salary)            # Initialize Job

# 3. Multilevel Inheritance
class Manager(EmployeePersonJob):  # Inherits from EmployeePersonJob
    def __init__(self, name, salary, department):
        EmployeePersonJob.__init__(self, name, salary)  # Explicitly initialize EmployeePersonJob
        self.department = department

# 4. Hierarchical Inheritance
class AssistantManager(EmployeePersonJob):  # Inherits from EmployeePersonJob
    def __init__(self, name, salary, team_size):
        EmployeePersonJob.__init__(self, name, salary)  # Explicitly initialize EmployeePersonJob
        self.team_size = team_size

# 5. Hybrid Inheritance (Multiple + Multilevel)
class SeniorManager(Manager, AssistantManager):  # Inherits from both Manager and AssistantManager
    def __init__(self, name, salary, department, team_size):
        Manager.__init__(self, name, salary, department)        # Initialize Manager
        AssistantManager.__init__(self, name, salary, team_size)  # Initialize AssistantManager

# Creating objects to show inheritance

# Single Inheritance
emp = Employee("John", 40000)
print(emp.name, emp.salary)

# Multiple Inheritance
emp2 = EmployeePersonJob("Alice", 50000)
print(emp2.name, emp2.salary)

# Multilevel Inheritance
mgr = Manager("Bob", 60000, "HR")
print(mgr.name, mgr.salary, mgr.department)

# Hierarchical Inheritance
asst_mgr = AssistantManager("Charlie", 45000, 10)
print(asst_mgr.name, asst_mgr.salary, asst_mgr.team_size)

# Hybrid Inheritance
sen_mgr = SeniorManager("David", 70000, "Finance", 20)
print(sen_mgr.name, sen_mgr.salary, sen_mgr.department, sen_mgr.team_size)

Output
John 40000
Alice 50000
Bob 60000 HR
Charlie 45000 10
David 70000 Finance 20

Explanation:

  1. Single Inheritance: Employee inherits from Person, adding a salary attribute.
  2. Multiple Inheritance: EmployeePersonJob inherits from both Employee and Job, allowing access to both name and salary.
  3. Multilevel Inheritance: Manager inherits from EmployeePersonJob, which already includes Employee and Job.
  4. Hierarchical Inheritance: AssistantManager also inherits from EmployeePersonJob, demonstrating multiple child classes inheriting from the same parent.
  5. Hybrid Inheritance: SeniorManager inherits from both Manager (multilevel) and AssistantManager (hierarchical), combining two inheritance types.

For more details please read this article: Types of inheritance in Python



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4