---
title: "What is meant by inheritance in python?"  
description: "What is meant by inheritance in python?"  
author: "Amrita Bhattacharjee"  
published: 2023-03-28  
updated: 2023-04-19  
canonical: https://www.mindstick.com/forum/157636/what-is-meant-by-inheritance-in-python  
category: "python"  
tags: ["python"]  
reading_time: 3 minutes  

---

# What is meant by inheritance in python?

What is meant by [inheritance](https://www.mindstick.com/articles/13095/inheritance-and-its-types) in [python](https://www.mindstick.com/articles/75378/simple-yet-useful-tips-when-using-python)?Give suitable example.

## Replies

### Reply by Aryan Kumar

Inheritance **allows us to define a class that inherits all the methods and properties from another class**. Parent class is the class being inherited from, also called base class. Child class is the class that inherits from another class, also called derived class.

When a child class inherits from a parent class, it automatically gains access to all the attributes and methods of the parent class. The child class can then add new attributes and methods, or modify the behavior of existing methods.

Inheritance allows us to reuse code and create classes that are more specialized than their parent classes. We can also create complex inheritance hierarchies with multiple levels of inheritance.

Here's a simple example of inheritance in Python:

```python
class Animal:
   def __init__(self, name):
       self.name = name

   def speak(self):
       raise NotImplementedError("Subclass must implement this abstract method")

class Dog(Animal):
   def speak(self):
       return "Woof!"
class Cat(Animal):
   def speak(self):
       return "Meow"
my_dog = Dog("Rufus")
print(my_dog.name)   # Output: Rufus
print(my_dog.speak())   # Output: Woof!
my_cat = Cat("Fluffy")
print(my_cat.name)   # Output: Fluffy
print(my_cat.speak())   # Output: Meow
```

### Reply by Krishnapriya Rajeev

Inheritance is the mechanism that allows us to *create a hierarchy of classes* that share a set of variables and methods by deriving it from another class. It is one of the core concepts in Object-Oriented Programming.

Every class in Python inherits from a built-in basic class called 'object'. The constructor, ie. the *__init__()* function of a class is invoked when we create an instance of that class. The variables that are described within *__init__()* are called the instance variables of the class.

```plaintext
# Base Class (inherits from object)
class Animal(object):
	def __init__(self,name,type):		# constructor
		self.name = name
		self.type = type

	def display(self):
		print(self.name, self.type)

# Derived Class (inherits from Animal)
class Cat(Animal):
	def display2:
		print("This is inside derived class.")

a = Animal("animal","organism")	# instance of base class
a.display()

c = Cat("cat","mammal")			# instance of derived class
c.display()
c.display2()

# output = animal organism
	     cat mammal
	     This is inside derived class.
```

There are two types of inheritance in Python:\
1. Single inheritance: This is when the child class inherits from only one parent class, an example of which was shown above.

2. Multiple inheritances: This is when the child class inherits from more than one parent class. Example:

```plaintext
# 1st Base Class
class Class1(object):
	def __init__(self,name):		# constructor
		self.name = name

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

# 2nd Base Class
class Class2(object):
	def __init__(self,num):			# constructor
		self.num = num

	def square(self):
		print(self.num**2)

class DerivedClass(Class1, Class2):
	def __init__(self,name,num):
		Class1.__init__(self,name)	# invoking constructor of 1st base class
		Class2.__init__(self,num)	# invoking constructor of 2nd base class

d = DerivedClass("John",5)
d.display()
d.square()

# output = John
	     25
```

3. Multilevel inheritance: This is when the child class inherits from a parent class, and another class inherits from that child class


---

Original Source: https://www.mindstick.com/forum/157636/what-is-meant-by-inheritance-in-python

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
