


Khusboo Tayal
Inheritance is one of the fundamental features of Object-Oriented Programming (OOP) in Python. It allows you to create a new class that inherits the properties and behaviors (methods and attributes) of an existing class.
Using inheritance, you can write cleaner, reusable, and scalable code. This tutorial will guide you through the basics of inheritance in Python, how to implement it, and when to use it.
Inheritance enables a new class (called a child class or derived class) to inherit methods and attributes from an existing class (called a parent class or base class).
class Parent:
# parent class members
class Child(Parent):
# child class membersA parent class contains the common attributes and methods.
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
print(f"{self.name} makes a sound")
The child class inherits from the parent and can add or override methods.
class Dog(Animal):
def speak(self):
print(f"{self.name} barks")
d = Dog("Buddy")
d.speak() # Output: Buddy barksThe super() function is used to call the constructor or methods of the parent class from the child class.
class Cat(Animal):
def __init__(self, name, color):
super().__init__(name) # calling parent constructor
self.color = color
def speak(self):
print(f"{self.name} meows and is {self.color}")
Python supports several types of inheritance:
One child class inherits from one parent class.
class A:
pass
class B(A):
passA class inherits from more than one parent class.
class A:
pass
class B:
pass
class C(A, B):
passA class is derived from a child class, which is also derived from another class.
class A:
pass
class B(A):
pass
class C(B):
passMultiple child classes inherit from a single parent class.
class Parent:
pass
class Child1(Parent):
pass
class Child2(Parent):
passA child class can override a method defined in the parent class.
class Animal:
def speak(self):
print("Animal speaks")
class Dog(Animal):
def speak(self):
print("Dog barks")
print(issubclass(Dog, Animal)) # True print(isinstance(d, Dog)) # True
class Vehicle:
def __init__(self, brand):
self.brand = brand
def start(self):
print(f"{self.brand} vehicle started.")
class Car(Vehicle):
def start(self):
print(f"{self.brand} car is now running.")
my_car = Car("Toyota")
my_car.start() # Output: Toyota car is now running.
Avoid inheritance when:
Inheritance in Python is a powerful concept that helps developers create flexible, modular, and reusable code. By leveraging parent and child classes, you can manage shared logic more effectively and build scalable applications using OOP principles.
Q1: Can a child class override the parent class constructor?
Yes, and you can use super() to call the parent constructor if needed.
Q2: Can a class inherit from multiple classes in Python?
Yes, Python supports multiple inheritance.
Q3: What's the difference between isinstance() and issubclass()?
isinstance() checks object-to-class relation, issubclass() checks class-to-class inheritance.
Subscribe to our newsletter to get the latest Python tutorials, project ideas, and updates right to your inbox.
#python #objectorientedprogramming #pythoninheritance #pythontutorial #pythonforbeginners #oopinpython #codingbasics #reusablecode #pythonclass