---
title: "Explain the Python Inheritance"  
description: "Explain the Python Inheritance"  
author: "Manish Kumar"  
published: 2025-10-27  
updated: 2025-10-27  
canonical: https://www.mindstick.com/interview/34396/explain-the-python-inheritance  
category: "python"  
tags: ["python-3.4", "Python 3"]  
reading_time: 5 minutes  

---

# Explain the Python Inheritance

> **Inheritance** is one of the main principles of [**Object-Oriented Programming (OOP)**](https://www.mindstick.com/articles/337949/object-oriented-programming-in-python).\
> It allows a class (called the **child** or **subclass**) to **inherit attributes and methods** from another class (called the **parent** or **base class**).

This helps you **reuse code**, **extend functionality**, and **organize your program** better.

## Basic Example

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

    def speak(self):
        return "Some generic sound"

# Child Class
class Dog(Animal):
    def speak(self):
        return "Woof!"

# Another Child Class
class Cat(Animal):
    def speak(self):
        return "Meow!"

# Using the classes
dog = Dog("Buddy")
cat = Cat("Kitty")

print(dog.name, "says:", dog.speak())  # Buddy says: Woof!
print(cat.name, "says:", cat.speak())  # Kitty says: Meow!
```

### Explanation:

- `Animal` is the **parent class**.
- `Dog` and `Cat` are **child classes** that inherit from `Animal`.
- They **override** the `speak()` method to provide their own behavior.

## Syntax of Inheritance

```python
class ChildClass(ParentClass):
    # child class body
    pass
```

If you don’t specify a parent, the class inherits from Python’s built-in `object` by default.

## Types of Inheritance in Python

**Single Inheritance**\
→ One child inherits from one parent.

```python
class A:
    pass
class B(A):
    pass
```

**Multiple Inheritance**\
→ One child inherits from multiple parents.

```python
class A:
    pass
class B:
    pass
class C(A, B):
    pass
```

**Multilevel Inheritance**\
→ A class inherits from a child class.

```python
class A:
    pass
class B(A):
    pass
class C(B):
    pass
```

**Hierarchical Inheritance**\
→ Multiple children inherit from the same parent.

```python
class Parent:
    pass
class Child1(Parent):
    pass
class Child2(Parent):
    pass
```

**Hybrid Inheritance**\
→ A combination of multiple inheritance types.

## `super()` Function

You can use `super()` to call methods or constructors from the parent class.

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

class Dog(Animal):
    def __init__(self, name, breed):
        super().__init__(name)   # Call parent constructor
        self.breed = breed

dog = Dog("Rex", "Labrador")
print(dog.name, dog.breed)
```

## Method Resolution Order (MRO)

In multiple inheritance, Python uses **MRO** (Method Resolution Order) to decide which parent class’s method to call first.

You can check it using:

```python
print(ClassName.__mro__)
```

## Advantages of Inheritance

- Promotes **code reuse**
- Makes **maintenance easier**
- Enables **extensibility** (easy to add new features)
- Supports **polymorphism** (same interface, different behavior)

## When *Not* to Use Inheritance

- When classes **don’t share a logical relationship**
- When **composition** (having objects as members) is a better fit

Example:

```python
# Instead of inheritance
class Engine:
    pass

class Car:
    def __init__(self):
        self.engine = Engine()  # Composition (has-a relationship)
```

## Answers

### Answer by Manish Kumar

> **Inheritance** is one of the main principles of [**Object-Oriented Programming (OOP)**](https://www.mindstick.com/articles/337949/object-oriented-programming-in-python).\
> It allows a class (called the **child** or **subclass**) to **inherit attributes and methods** from another class (called the **parent** or **base class**).

This helps you **reuse code**, **extend functionality**, and **organize your program** better.

## Basic Example

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

    def speak(self):
        return "Some generic sound"

# Child Class
class Dog(Animal):
    def speak(self):
        return "Woof!"

# Another Child Class
class Cat(Animal):
    def speak(self):
        return "Meow!"

# Using the classes
dog = Dog("Buddy")
cat = Cat("Kitty")

print(dog.name, "says:", dog.speak())  # Buddy says: Woof!
print(cat.name, "says:", cat.speak())  # Kitty says: Meow!
```

### Explanation:

- `Animal` is the **parent class**.
- `Dog` and `Cat` are **child classes** that inherit from `Animal`.
- They **override** the `speak()` method to provide their own behavior.

## Syntax of Inheritance

```python
class ChildClass(ParentClass):
    # child class body
    pass
```

If you don’t specify a parent, the class inherits from Python’s built-in `object` by default.

## Types of Inheritance in Python

**Single Inheritance**\
→ One child inherits from one parent.

```python
class A:
    pass
class B(A):
    pass
```

**Multiple Inheritance**\
→ One child inherits from multiple parents.

```python
class A:
    pass
class B:
    pass
class C(A, B):
    pass
```

**Multilevel Inheritance**\
→ A class inherits from a child class.

```python
class A:
    pass
class B(A):
    pass
class C(B):
    pass
```

**Hierarchical Inheritance**\
→ Multiple children inherit from the same parent.

```python
class Parent:
    pass
class Child1(Parent):
    pass
class Child2(Parent):
    pass
```

**Hybrid Inheritance**\
→ A combination of multiple inheritance types.

## `super()` Function

You can use `super()` to call methods or constructors from the parent class.

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

class Dog(Animal):
    def __init__(self, name, breed):
        super().__init__(name)   # Call parent constructor
        self.breed = breed

dog = Dog("Rex", "Labrador")
print(dog.name, dog.breed)
```

## Method Resolution Order (MRO)

In multiple inheritance, Python uses **MRO** (Method Resolution Order) to decide which parent class’s method to call first.

You can check it using:

```python
print(ClassName.__mro__)
```

## Advantages of Inheritance

- Promotes **code reuse**
- Makes **maintenance easier**
- Enables **extensibility** (easy to add new features)
- Supports **polymorphism** (same interface, different behavior)

## When *Not* to Use Inheritance

- When classes **don’t share a logical relationship**
- When **composition** (having objects as members) is a better fit

Example:

```python
# Instead of inheritance
class Engine:
    pass

class Car:
    def __init__(self):
        self.engine = Engine()  # Composition (has-a relationship)
```


---

Original Source: https://www.mindstick.com/interview/34396/explain-the-python-inheritance

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
