What is inheritance in OOP?

Understanding inheritance in Object-Oriented Programming (OOP) is like building a family tree for your code. It allows you to create new classes based on existing ones, promoting reusability and organization. Let's dive into what inheritance is and how it works in OOP.

Inheritance

Inheritance is a fundamental concept in OOP that lets a new class (called a subclass or derived class) inherit properties and behaviors from an existing class (called a superclass or base class). This mechanism promotes code reusability and establishes a natural hierarchy between classes, making your code more organized and easier to maintain.

Example

Imagine you’re designing a game with different types of characters. You can create a base class called Character that includes common attributes and methods shared by all characters, such as name, health, and move(). Then, you can create subclasses like Warrior, Mage, and Archer that inherit from Character and add their unique attributes and behaviors.

# Base class class Character: def __init__(self, name, health): self.name = name self.health = health def move(self): print(f"{self.name} moves to a new location.") # Subclass inheriting from Character class Warrior(Character): def __init__(self, name, health, strength): super().__init__(name, health) self.strength = strength def attack(self): print(f"{self.name} attacks with strength {self.strength}.") # Subclass inheriting from Character class Mage(Character): def __init__(self, name, health, mana): super().__init__(name, health) self.mana = mana def cast_spell(self): print(f"{self.name} casts a spell using {self.mana} mana.")

In this example:

  • Warrior and Mage inherit from the Character class.
  • They reuse the name and health attributes and the move() method from Character.
  • Each subclass adds its own unique attributes (strength for Warrior and mana for Mage) and methods (attack() and cast_spell() respectively).

Benefits of Inheritance

  • Reusability: Inheritance allows you to reuse existing code, reducing redundancy and effort.
  • Organization: It helps in organizing code logically, making it easier to manage and understand.
  • Extensibility: You can easily extend existing classes to add new features without modifying the original class.
  • Maintainability: Changes made to the base class automatically propagate to subclasses, simplifying maintenance.

To deepen your understanding of inheritance and other OOP concepts, consider enrolling in the following courses from DesignGurus.io:

These courses offer comprehensive insights and practical examples to help you master Object-Oriented Programming principles and excel in your technical interviews.

TAGS
Coding Interview
CONTRIBUTOR
Design Gurus Team
-

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
What is industry specific technology?
Why should we hire you as a front-end developer?
What is the data engineer role?
What is the '-->' operator in C/C++?
What is the salary of senior staff in Zscaler?
Can you still get the job if you fail the coding interview?
Related Courses
Grokking the Coding Interview: Patterns for Coding Questions course cover
Grokking the Coding Interview: Patterns for Coding Questions
The 24 essential patterns behind every coding interview question. Available in Java, Python, JavaScript, C++, C#, and Go. The most comprehensive coding interview course with 543 lessons. A smarter alternative to grinding LeetCode.
4.6
Discounted price for Your Region

$197

Grokking Modern AI Fundamentals course cover
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
3.9
Discounted price for Your Region

$72

Grokking Data Structures & Algorithms for Coding Interviews course cover
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
4
Discounted price for Your Region

$78

Design Gurus logo
One-Stop Portal For Tech Interviews.
Copyright © 2026 Design Gurus, LLC. All rights reserved.