What is inheritance in Python?

Inheritance is a fundamental concept in object-oriented programming (OOP) that allows you to create a new class (derived class or subclass) based on an existing class (base class or superclass). The derived class inherits the attributes and methods of the base class, which can then be extended or overridden to create specialized behavior.

In Python, you can define an inheritance relationship by specifying the base class in the definition of the derived class. The derived class inherits all the attributes (variables) and methods (functions) of the base class, which can be used directly within the derived class or overridden to provide customized behavior.

Here's the general syntax for defining a derived class with inheritance in Python:

class BaseClass:
    # Base class attributes and methods

class DerivedClass(BaseClass):
    # Derived class attributes and methods

Let's explore some key aspects of inheritance in Python:

Accessing base class members: Within the derived class, you can access the attributes and methods of the base class using the super() function. This allows you to call the base class methods or access its attributes. Additionally, you can access them directly using the derived class instance.

Overriding methods: The derived class can provide its own implementation of a method with the same name as the base class method. This is known as method overriding. When a method is called on an instance of the derived class, the overridden method in the derived class is executed instead of the base class method.

Adding new attributes and methods: In the derived class, you can add new attributes and methods specific to the derived class. These attributes and methods are in addition to those inherited from the base class.

Inheritance hierarchy: In Python, you can have multiple levels of inheritance, forming an inheritance hierarchy. A derived class can further serve as a base class for another derived class, and so on, creating a hierarchical structure.

Inheritance types: Python supports both single inheritance (a derived class inherits from a single base class) and multiple inheritance (a derived class inherits from multiple base classes).

Here's an example that demonstrates inheritance in Python:

class Animal:
    def __init__(self, name): = name

    def sound(self):
        print("Animal makes a sound.")

class Dog(Animal):
    def sound(self):
        print("Dog barks.")

class Cat(Animal):
    def sound(self):
        print("Cat meows.")

# Creating instances of the derived classes
dog = Dog("Buddy")
cat = Cat("Misty")

# Accessing base class method
dog.sound()  # Output: Dog barks.
cat.sound()  # Output: Cat meows.

# Accessing base class attribute
print(  # Output: Buddy
print(  # Output: Misty

In the above example, the Animal class is the base class, and the Dog and Cat classes are derived classes. The derived classes override the sound() method of the base class to provide their specific implementation. The instances of the derived classes inherit the name attribute from the base class and have access to the overridden method.

Inheritance allows for code reuse, promotes modularity, and enables the creation of specialized classes based on existing ones. It is a powerful feature of object-oriented programming that enhances code organization and maintainability.

Related Questions You Might Be Interested