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
Arslan Ahmad
Arslan Ahmad
ex-FAANG engineering manager and author or Grokking series.

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 to expect in DocuSign system design interview?
What is STLC?
Which software engineer has the highest salary?
What is API in Android?
Can You Use LeetCode Offline? And How to Practice Without Internet
LeetCode has no offline mode because every submission is graded on its servers, but you can still practice offline with a local editor and your own test cases.
Which interview slot is best?
Related Courses
New
Grokking Agentic Design Patterns course cover
Grokking Agentic Design Patterns
Design agents that act safely, recover from failure, and justify their cost.
4.9
(561 learners)
Discounted price for Your Region

$98

New
Grokking the AI System Design Interview course cover
Grokking the AI System Design Interview
Learn to design AI systems the way interviewers expect: classic ML products, LLM and RAG architectures, and agentic systems, all through the lens of the system design interview.
4.6
(3,192 learners)
Discounted price for Your Region

$98

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

Design Gurus logo
One-Stop Portal For Tech Interviews.
About Us
Our Team
Careers
Partners
Contact Us
Become Affiliate
Copyright © 2026 Design Gurus, LLC. All rights reserved.