Inheritance | Dataplexa

Inheritance in Python

Inheritance is one of the most powerful features of Object-Oriented Programming. It allows one class (child class) to reuse the properties and behaviors of another class (parent class). This makes your code cleaner, avoids duplication, and helps you build large systems easily.

What Is Inheritance?

Inheritance means creating a new class using an existing class as the foundation. The child class automatically gets all the attributes and methods of the parent class. You can also add new features or override old ones to customize behavior.

Why Use Inheritance?

Inheritance helps you write less code because common functionality is stored in one base class. It improves maintainability since updates in the parent class automatically reflect in child classes. It also organizes your project better by grouping shared logic in a single place.

Basic Syntax of Inheritance

To inherit a class, simply pass the parent class name inside parentheses while defining the child class.

class Parent:
    pass

class Child(Parent):
    pass

Now Child has everything that Parent contains, even without defining anything inside it.

Example: Inheriting Attributes and Methods

The child class automatically gains access to the parent’s attributes and functions, making code highly reusable. This helps prevent writing the same logic again in multiple classes.

class Animal:
    def speak(self):
        print("This animal makes a sound")

class Dog(Animal):
    pass

d = Dog()
d.speak()

Even though Dog has no methods, it can still use speak() from Animal.

Adding New Methods in a Child Class

Child classes can extend the parent class by adding their own unique behaviors. This lets each child class provide specialized functionality while still inheriting the parent’s structure.

class Animal:
    def speak(self):
        print("Animal sound")

class Cat(Animal):
    def meow(self):
        print("Meow!")

c = Cat()
c.speak()
c.meow()

Overriding Methods

A child class can replace the behavior of a parent method using the same method name. This is helpful when the parent class has general logic and the child class needs a custom version.

class Animal:
    def speak(self):
        print("Animal sound")

class Dog(Animal):
    def speak(self):
        print("Bark!")

d = Dog()
d.speak()

The child version overrides the parent version completely.

Using super() to Access Parent Methods

The super() function allows the child class to call methods from the parent class. This is useful when you want to extend — but not replace — the parent’s functionality.

class Vehicle:
    def start(self):
        print("Vehicle is starting")

class Car(Vehicle):
    def start(self):
        super().start()
        print("Car engine is now running")

c = Car()
c.start()

Here, the child method first runs the parent method, then adds new behavior.

Types of Inheritance in Python

Python supports several forms of inheritance depending on your project structure. Understanding them helps you organize large applications efficiently.

1. Single Inheritance

The child has only one parent class.

2. Multiple Inheritance

A class inherits from more than one parent class at the same time.

3. Multilevel Inheritance

A child class becomes a parent of another class, forming a chain.

class A:
    pass

class B(A):
    pass

class C(B):
    pass

4. Hierarchical Inheritance

Multiple child classes inherit from the same parent class.

5. Hybrid Inheritance

A combination of two or more inheritance types.

Real-World Example

Inheritance is used in banking systems, e-commerce platforms, games, and automation tools. For example, different payment methods may inherit from a common Payment class and extend their specific logic.

class Payment:
    def pay(self):
        print("Processing payment")

class CreditCard(Payment):
    def verify(self):
        print("Verifying card details")

p = CreditCard()
p.pay()
p.verify()

📝 Practice Exercises


Exercise 1

Create a parent class Bird with a method fly(). Create a child class Sparrow that inherits and uses the method.

Exercise 2

Create a parent class Appliance with a method turn_on(). Create a child class WashingMachine that overrides the method.

Exercise 3

Create a multilevel inheritance chain with three classes and print one method from the top class.

Exercise 4

Create a child class that uses super() to call a parent method and then add additional behavior.


✅ Practice Answers


Answer 1

class Bird:
    def fly(self):
        print("Bird is flying")

class Sparrow(Bird):
    pass

s = Sparrow()
s.fly()

Answer 2

class Appliance:
    def turn_on(self):
        print("Turning on appliance")

class WashingMachine(Appliance):
    def turn_on(self):
        print("Washing machine starting...")

w = WashingMachine()
w.turn_on()

Answer 3

class A:
    def show(self):
        print("From class A")

class B(A):
    pass

class C(B):
    pass

c = C()
c.show()

Answer 4

class Device:
    def start(self):
        print("Device is starting")

class Phone(Device):
    def start(self):
        super().start()
        print("Phone booting up")

p = Phone()
p.start()