Inheritance in Python | Learn How One Class Can Use Another



๐Ÿ”น What is Inheritance?

  • Inheritance means a class (child) can use the data and functions from another class (parent).
  • This helps us reuse code and build bigger programs easily.

๐Ÿ“Œ Real-life example: A child gets features from parents โ€” like eyes, language, habits.
Same way, a class gets methods and data from another class.


๐Ÿ”น Why Use Inheritance?

โœ… Reuse old code
โœ… Add new features easily
โœ… Clean and simple design
โœ… Avoid writing same code again


๐Ÿ”น Basic Example: Simple Inheritance

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

class Dog(Animal):  # Inherits Animal
    def bark(self):
        print("Dog barks")

d = Dog()
d.speak()  # From parent
d.bark()   # From child


๐Ÿ”ธ Types of Inheritance in Python (with Examples)

Python supports 5 types of inheritance:


1. Single Inheritance

One parent class โ†’ One child class

class Parent:
    def show(self):
        print("This is Parent")

class Child(Parent):
    def display(self):
        print("This is Child")

c = Child()
c.show()
c.display()


2. Multilevel Inheritance

Parent โ†’ Child โ†’ Grandchild

class Grandfather:
    def house(self):
        print("Grandfather's house")

class Father(Grandfather):
    def car(self):
        print("Father's car")

class Son(Father):
    def bike(self):
        print("Son's bike")

s = Son()
s.house()
s.car()
s.bike()


3. Hierarchical Inheritance

One parent โ†’ Many children

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

class Dog(Animal):
    def bark(self):
        print("Dog barks")

class Cat(Animal):
    def meow(self):
        print("Cat meows")

d = Dog()
c = Cat()
d.sound(); d.bark()
c.sound(); c.meow()


4. Multiple Inheritance

Child class inherits from two or more parent classes

class Father:
    def job(self):
        print("Father is a teacher")

class Mother:
    def cook(self):
        print("Mother cooks food")

class Child(Father, Mother):
    def play(self):
        print("Child plays cricket")

c = Child()
c.job()
c.cook()
c.play()


5. Hybrid Inheritance

Combination of multiple types (e.g., multiple + multilevel)

class A:
    def a(self):
        print("Class A")

class B(A):
    def b(self):
        print("Class B")

class C:
    def c(self):
        print("Class C")

class D(B, C):  # Combines B (from A) + C
    def d(self):
        print("Class D")

obj = D()
obj.a()
obj.b()
obj.c()
obj.d()


๐Ÿ”น Using super() to Access Parent Constructor

class Person:
    def __init__(self, name):
        self.name = name

class Student(Person):
    def __init__(self, name, grade):
        super().__init__(name)  # call parent __init__
        self.grade = grade

s = Student("Arun", "A")
print(s.name, s.grade)


๐Ÿ“ Practice Questions with Answers

โ“ Q1: Create a base class Vehicle, and Car, Bike should inherit from it.

โœ… Answer:

class Vehicle:
    def start(self):
        print("Vehicle starts")

class Car(Vehicle):
    def drive(self):
        print("Car drives")

class Bike(Vehicle):
    def ride(self):
        print("Bike rides")

c = Car()
c.start(); c.drive()

b = Bike()
b.start(); b.ride()


โ“ Q2: Show multilevel inheritance using Company, Manager, Employee.

โœ… Answer:

class Company:
    def company_name(self):
        print("XYZ Ltd")

class Manager(Company):
    def manage(self):
        print("Manages team")

class Employee(Manager):
    def work(self):
        print("Works on project")

e = Employee()
e.company_name()
e.manage()
e.work()


๐Ÿงพ Summary Table

Inheritance TypeStructure Example
SingleA โ†’ B
MultilevelA โ†’ B โ†’ C
HierarchicalA โ†’ B, A โ†’ C
MultipleA + B โ†’ C
HybridCombination of above

๐Ÿ“ฃ Coming Up Next:

โžก๏ธ Polymorphism in Python | One Name, Many Forms (Easy Guide)


Comments

Leave a Reply

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