Hackforge Academy

Category: python

Inheritance in Python

Published on 24 Jul 2026

Explanation

Inheritance is one of the fundamental principles of Object-Oriented Programming (OOP). It allows a new class (called the child or

Code:

class Person:

    def __init__(self, name, age):
        self.name = name
        self.age = age

    def display(self):
        print('Name:', self.name)
        print('Age:', self.age)


class Student(Person):

    def __init__(self, name, age, course):
        super().__init__(name, age)
        self.course = course

    def show_course(self):
        print('Course:', self.course)


student = Student('John', 21, 'Python')
student.display()
student.show_course()

Explanation

Polymorphism means 'many forms'. It allows the same method name to behave differently depending on the object that calls it.

Code:

class Animal:

    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')


animals = [Dog(), Cat()]

for animal in animals:
    animal.sound()

Explanation

Encapsulation is the process of combining data and methods into a single unit (class) while restricting direct access to internal

Code:

class BankAccount:

    def __init__(self, balance):
        self.__balance = balance

    def deposit(self, amount):
        self.__balance += amount

    def withdraw(self, amount):
        if amount <= self.__balance:
            self.__balance -= amount
        else:
            print('Insufficient Balance')

    def get_balance(self):
        return self.__balance


account = BankAccount(10000)
account.deposit(2000)
account.withdraw(1500)
print('Balance:', account.get_balance())

Explanation

Abstraction is the process of hiding implementation details and exposing only the essential functionality to users. Python supports abstraction through

Code:

from abc import ABC, abstractmethod


class Vehicle(ABC):

    @abstractmethod
    def start(self):
        pass


class Car(Vehicle):

    def start(self):
        print('Car Started')


class Bike(Vehicle):

    def start(self):
        print('Bike Started')


car = Car()
bike = Bike()

car.start()
bike.start()

Explanation

Modern software applications rely heavily on the four pillars of Object-Oriented Programming. Inheritance enables code reuse, polymorphism allows different implementations

Code:

from abc import ABC, abstractmethod


class Payment(ABC):

    @abstractmethod
    def pay(self, amount):
        pass


class CreditCard(Payment):

    def pay(self, amount):
        print(f'Paid ₹{amount} using Credit Card')


class UPI(Payment):

    def pay(self, amount):
        print(f'Paid ₹{amount} using UPI')


class Cash(Payment):

    def pay(self, amount):
        print(f'Paid ₹{amount} using Cash')


payments = [CreditCard(), UPI(), Cash()]

for payment in payments:
    payment.pay(2500)

🚀 Learn Spring Boot with real-world projects

💡 Build REST APIs step by step

🧠 Improve backend development skills

🎯 Get career-ready practical training

Join Our Free WhatsApp Community

Direct access to niche-specific mentors and peers on WhatsApp.

🐍

Python Community

Discuss Django, FastAPI, AI integration, and automation scripts with 15k+ developers.

Join Python Community
⚛️

React Community

Master Next.js, Framer Motion, and State Management. Share your latest UI components.

Join React Community

Java Community

Deep dives into Spring Boot, Microservices architecture, and high-performance backend ops.

Join Java Community