TV remote β You donβt know how it works inside
Medicine capsule β It hides the inner parts
πΉ Example of Abstraction:
from abc import ABC, abstractmethod
class Payment(ABC):
@abstractmethod
def pay(self):
pass
class UPI(Payment):
def pay(self):
print("Payment using UPI")
p = UPI()
p.pay()
β The user only knows they are using UPI. How it works inside is hidden.
πΉ Example of Encapsulation:
class Student:
def __init__(self):
self.__marks = 0 # private variable
def set_marks(self, m):
self.__marks = m
def get_marks(self):
return self.__marks
s = Student()
s.set_marks(95)
print(s.get_marks()) # Output: 95
β The marks are protected. We use methods to access them.
π§Ύ Summary Table
Key Point
Abstraction
Encapsulation
Purpose
Hides unnecessary details
Hides sensitive data
How it’s done
With abstract classes & methods
Using private variables & methods
Access control?
No
Yes (__private)
Real-life comparison
TV remote
Medicine capsule
π§ Easy Tip to Remember
π Abstraction = “Donβt show how it works” (Focus on What)
π Encapsulation = “Donβt let others touch my data” (Focus on How safe)
Hiding the complex details and showing only the important parts.
It helps us to:
Focus on what an object does
Not worry about how it does it
πΈ Real-Life Example:
Think about a TV remote:
You press the power button to turn on the TV.
You donβt need to know how electricity flows inside.
π’ This is abstraction β showing only the button (interface), hiding the working inside.
πΉ Abstraction in Python
We use:
Abstract Base Class (ABC)
The abc module
πΉ How to Use Abstraction in Python
Import ABC and abstractmethod
Create a class that inherits from ABC
Use @abstractmethod to define abstract methods
Subclass must override these methods
πΉ Example:
from abc import ABC, abstractmethod
class Animal(ABC): # Abstract class
@abstractmethod
def sound(self): # Abstract method
pass
class Dog(Animal):
def sound(self):
print("Dog barks")
class Cat(Animal):
def sound(self):
print("Cat meows")
d = Dog()
c = Cat()
d.sound()
c.sound()
π’ Output:
Dog barks
Cat meows
πΉ Key Rules of Abstraction:
You cannot create an object of abstract class.
Child class must implement (override) abstract methods.
Abstract class can also have normal methods.
πΉ Abstract Class with Normal Method
from abc import ABC, abstractmethod
class Vehicle(ABC):
@abstractmethod
def start(self):
pass
def fuel_type(self):
print("Fuel type is petrol")
class Car(Vehicle):
def start(self):
print("Car is starting...")
c = Car()
c.start()
c.fuel_type()
π Practice Questions with Answers
β Q1: Create an abstract class Shape with abstract method area(). Implement it in Rectangle and Circle.
β Answer:
from abc import ABC, abstractmethod
class Shape(ABC):
@abstractmethod
def area(self):
pass
class Rectangle(Shape):
def area(self):
print("Area = length Γ width")
class Circle(Shape):
def area(self):
print("Area = Ο Γ radiusΒ²")
r = Rectangle()
c = Circle()
r.area()
c.area()
β Q2: Make an abstract class Device with turn_on() and turn_off() methods.
β Answer:
from abc import ABC, abstractmethod
class Device(ABC):
@abstractmethod
def turn_on(self):
pass
@abstractmethod
def turn_off(self):
pass
class Laptop(Device):
def turn_on(self):
print("Laptop is turning on")
def turn_off(self):
print("Laptop is shutting down")
l = Laptop()
l.turn_on()
l.turn_off()
π§Ύ Summary Table
Term
Meaning
Abstraction
Hiding how things work, showing only whatβs needed
Abstract Class
A class with abstract methods
Abstract Method
Method with no body (empty function)
@abstractmethod
Used to create abstract methods
π― Quick Recap
Abstraction = Hiding details, showing only the essentials.
Use abc module, ABC, and @abstractmethod.
Helps write clean, structured, and safe code.
π£ Coming Up Next:
β‘οΈ Difference Between Abstraction and Encapsulation in Python (Simple Words)
A child class (subclass) writes its own version of a method that already exists in the parent class (superclass).
It helps the child class change or improve the behavior of a parent method.
The method name must be same in both classes.
πΈ Real-Life Example:
Imagine a general printer prints in black & white. But a color printer overrides that function and prints in color.
πΉ Basic Example of Method Overriding:
class Animal:
def speak(self):
print("Animal speaks")
class Dog(Animal):
def speak(self): # Overriding the parent's method
print("Dog barks")
d = Dog()
d.speak() # Output: Dog barks
β Dog class has its own version of the speak() method.
πΉ Why Use Method Overriding?
β To customize behavior β To add extra work in child class β To make code flexible and clean
πΉ Calling Parent Method Using super()
Sometimes we want to call the original (parent) method also:
class Animal:
def speak(self):
print("Animal speaks")
class Dog(Animal):
def speak(self):
super().speak() # Call parent method
print("Dog barks")
d = Dog()
d.speak()
π’ Output:
Animal speaks
Dog barks
πΉ Another Example: Bank Account
class Account:
def show_balance(self):
print("Balance is $1000")
class SavingsAccount(Account):
def show_balance(self):
print("Savings balance is $1500")
a = SavingsAccount()
a.show_balance()
π Practice Questions with Answers
β Q1: Create a class Shape with a method draw(). Override it in Circle and Square.
β Answer:
class Shape:
def draw(self):
print("Drawing a shape")
class Circle(Shape):
def draw(self):
print("Drawing a circle")
class Square(Shape):
def draw(self):
print("Drawing a square")
c = Circle()
s = Square()
c.draw()
s.draw()
β Q2: Create a class Employee with method work(). Override it in Manager and Developer.
β Answer:
class Employee:
def work(self):
print("Employee works")
class Manager(Employee):
def work(self):
print("Manager manages the team")
class Developer(Employee):
def work(self):
print("Developer writes code")
m = Manager()
d = Developer()
m.work()
d.work()
π§Ύ Summary
Term
Meaning
Overriding
Child class redefines parent method
Method Name
Must be the same in both classes
Use of super()
To call the parent class method from child
π― Quick Recap
Method Overriding = Changing a parent method in the child class.
Useful when child class needs a different behavior.
super() helps to use parentβs method with the new one.
β len() function works with string, list, tuple β same function name, many forms.
π Practice Questions with Answers
β Q1: Create a method called greet() in two classes: English and Spanish.
β Answer:
class English:
def greet(self):
print("Hello")
class Spanish:
def greet(self):
print("Hola")
def say_hello(obj):
obj.greet()
e = English()
s = Spanish()
say_hello(e)
say_hello(s)
β Q2: Write a function that accepts any shape class and prints area using area() method.
β Answer:
class Circle:
def area(self):
print("Area of Circle: ΟrΒ²")
class Square:
def area(self):
print("Area of Square: sideΒ²")
def print_area(shape):
shape.area()
c = Circle()
s = Square()
print_area(c)
print_area(s)
π§Ύ Summary Table
Term
Meaning
Polymorphism
One method or function, many forms
Built-in
Functions like len(), +, etc.
User-defined
Methods in different classes with same name
π― Quick Recap
Polymorphism helps the same method name work in different ways.
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)