Python Inheritance

🐍 Python Inheritance — Complete Guide

Inheritance is a core concept of Object-Oriented Programming (OOP).
It allows a class (child) to inherit attributes and methods from another class (parent).


 1️⃣ Why Use Inheritance?

  • Reusability: Avoid rewriting code.

  • Extensibility: Add new features to existing classes.

  • Hierarchy: Represents real-world relationships (e.g., Vehicle → Car → ElectricCar).


 2️⃣ Basic Syntax

class Parent:
def greet(self):
print("Hello from Parent")
class Child(Parent):
pass

c = Child()
c.greet() # Hello from Parent

The Child class automatically inherits the greet() method from Parent.


🔹 3️⃣ Example: Single Inheritance

class Animal:
def sound(self):
print("Some generic sound")
class Dog(Animal):
def bark(self):
print(“Woof! Woof!”)

d = Dog()
d.sound() # inherited method
d.bark() # own method


🔹 4️⃣ Using super() to Call Parent Constructor

class Person:
def __init__(self, name):
self.name = name
class Employee(Person):
def __init__(self, name, salary):
super().__init__(name) # call parent constructor
self.salary = salary

e = Employee(“Vipul”, 50000)
print(e.name) # Vipul
print(e.salary) # 50000


🔹 5️⃣ Multiple Inheritance

A child class can inherit from more than one parent.

class Mother:
def skills(self):
print("Cooking")
class Father:
def skills(self):
print(“Driving”)

class Child(Mother, Father):
pass

c = Child()
c.skills() # Cooking (Mother is first in order)

Python uses Method Resolution Order (MRO) to determine which parent method to use.


🔹 6️⃣ Multilevel Inheritance

class Grandparent:
def legacy(self):
print("Inheritance from Grandparent")
class Parent(Grandparent):
def skill(self):
print(“Parent Skill”)

class Child(Parent):
def hobby(self):
print(“Child Hobby”)

c = Child()
c.legacy() # Inherited from Grandparent
c.skill() # Inherited from Parent
c.hobby() # Own method


🔹 7️⃣ Hierarchical Inheritance

Multiple child classes inherit from one parent class.

class Vehicle:
def wheels(self):
print("All vehicles have wheels")
class Car(Vehicle):
pass

class Bike(Vehicle):
pass

c = Car()
b = Bike()

c.wheels() # All vehicles have wheels
b.wheels() # All vehicles have wheels


🔹 8️⃣ Overriding Parent Methods

Child can override parent method:

class Animal:
def sound(self):
print("Some sound")
class Dog(Animal):
def sound(self):
print(“Woof! Woof!”)

d = Dog()
d.sound() # Woof! Woof!

Can still call parent method using super():

class Dog(Animal):
def sound(self):
super().sound()
print("Woof! Woof!")
d = Dog()
d.sound()

Output:

Some sound
Woof! Woof!

🔹 9️⃣ Summary Table

Type of Inheritance Example
Single Child inherits from one parent
Multiple Child inherits from multiple parents
Multilevel Grandparent → Parent → Child
Hierarchical One parent → multiple children
Hybrid Combination of multiple types

🔹 10️⃣ Example: Real-Life Use

class Employee:
def __init__(self, name, salary):
self.name = name
self.salary = salary
def details(self):
print(f”{self.name} earns {self.salary}“)

class Manager(Employee):
def bonus(self, bonus):
print(f”{self.name} receives bonus {bonus}“)

m = Manager(“Vipul”, 70000)
m.details() # inherited
m.bonus(10000) # own method


Inheritance makes code reusable, organized, and scalable.

CodeCapsule

Sanjit Sinha — Web Developer | PHP • Laravel • CodeIgniter • MySQL • Bootstrap Founder, CodeCapsule — Student projects & practical coding guides. Email: info@codecapsule.in • Website: CodeCapsule.in

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *