πΉ 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 Type | Structure Example |
|---|---|
| Single | A β B |
| Multilevel | A β B β C |
| Hierarchical | A β B, A β C |
| Multiple | A + B β C |
| Hybrid | Combination of above |
π£ Coming Up Next:
β‘οΈ Polymorphism in Python | One Name, Many Forms (Easy Guide)
