๐น 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)
Leave a Reply