Python Inheritance

About Python Inheritance

In Python, inheritance is to create a new class inheriting the attributes (properties) and methods of another class.

The new class that inherits is called the derived (or child) class.

The class from which attributes and methods are inherited is called the base (or parent) class.

Derived classes can override or extend the functionality of base class methods.

It allows developers to reuse code and simplifies code maintenance and enhancement.

Python Inheritance Syntax

Syntax of single class inheritance

class DerivedClass(BaseClass): The line defines inheritance. Here the name of base class 'BaseClass' is placed in the parentheses '()' after the name of derived class 'DerivedClass' when defining the derived class.

Syntax of multiple class inheritance

class DerivedClass(BaseClass1, BaseClass2):: The line defines multiple inheritance. Here base classes are seperated by comma in parentheses '()'.

Examples of Python Inheritance

Following example demontrates a simple inheritance where 'Dog' class inherites property and method from 'Animal' class.

Output of the above example
Buddy
Buddy says woof!
                                
Explanation of code
  • class Animal:: 'Animal' class is defined. It is used here as a base class.
  • def __init__(self, name): self.name = name: This initializes the Animal class with __init__() method which takes 'name' as a parameter and assigns it to the instance variable 'self.name'.
  • def speak(self, sound):: This defines 'speak' method with a parameter 'sound' that returns a string value (return f"{self.name} says {sound}!").
  • class Dog(Animal):: Here the 'Dog' class is defined inheriting from the 'Animal' class. 'Animal' is placed within parentheses '()' after 'Dog' class name.
  • pass: 'pass' is used here as no new attribute or method is added to the 'Dog' class.
  • dog = Dog("Buddy"): Dog("Buddy") is an instance of 'Dog' class with the name "Buddy". The 'name' attribute is inherited from the 'Animal' class and initialized with the value "Buddy" through the __init__() method of 'Animal' class.
  • print(dog.name): Please note that you can access the 'name' attribute of Animal class from the 'dog' object of Dog class.
  • print(dog.speak("woof")): Please note that you can access the 'speak' method of Animal class from the 'dog' object of Dog class.

Overriding Methods in the Derived Class

In Python, the derived class can override the method of base class for a specific implementation. In other words, the functionality of the method of base class can be replaced with new functionality in the drived class.

The overridden method in the derived class should have the same name and parameters as the method in the base class.

Example of method overriding in Python inheritance
Output of the above example
Animal makes sound.
Dog barks.
                                
Explanation of code
  • class Animal:: 'Animal' class is defined and here it is the base class.
  • def speak(self): return "Animal makes sound." is a method implemented in the base class. The 'speak' method returns "Animal makes sound.".
  • print(animalObj.speak()): This calls and prints the 'speak' method of 'Animal' class. The output is "Animal makes sound." .
  • class Dog(Animal): 'Dog' class is created with inheritance from the 'Animal' class. Here Dog class is a derived class.
  • def speak(self): return "Dog barks." : 'Dog' class overrides the 'speak' method of base class which returns "Dog barks." instead of "Animal makes sound.".
  • print(dog.speak()): This calls and prints the 'speak' method of 'Dog' class. The output is "Dog barks.".

Using the super() Function in Inheritance

In Python, the 'super()' function is used to call a method from the base class in the derived class.

It allows you to access the methods and properties of the base class without explicitly naming it.

It is commonly used in the __init__() method to initialize the attributes of the base class before the derived class adds or modifies its own attributes and behaviors.

This is particularly useful when you want to extend or modify the behavior of a method in the derived class while still retaining the functionality of the base class method.

Output of the above example
Buddy says woof!
The age of Buddy is 5
                                
Explanation of code
  • In the above example, Animal class is the base (parent) class and Dog is the derived (child) class.
  • def __init__(self, name, age):: The __init__() method of 'Dog' class takes two parameters 'name' and 'age'. The 'name' parameter is used to initialize the 'name' attribute of base class and 'age' parameter is used to initialize the 'age' attribute of derived class.
  • super().__init__(name) : The 'super()' function is used here to call the __init__() method of base class from the __init__() method of derived class. It initializes the 'name' attribute of base class with the value passed as 'name' parameter. Please note that the 'super()' method is called before initializing the 'age' attribute of derived class. This is because the base class attributes should be initialized before the derived class attributes.
  • super().speak("woof") : The 'super()' function is also used here to call the 'speak()' method of base class from the 'get_Age()' method of derived class. It calls the 'speak()' method of base class with the argument "woof". This demonstrates that you can call any method of base class from the derived class using 'super()' function.

Python Compiler For Live Practice

Practice the above Python code in the following Python Editor. Create more Python inheritance examples in the following Python code editor and click Execute button to check the output. Output will be displayed in Python Code Output section.

Python logo Python Editor
Python Code Output