Complete OOP in Python Notes (Detailed
Explanation)
1. What is OOP?
Object-Oriented Programming (OOP) is a programming paradigm where code is organized into objects.
Each object contains data (attributes) and behavior (methods). It helps in building modular, reusable,
and scalable applications.
2. Class
A class is a blueprint or template used to create objects. It defines properties and behaviors that objects
created from it will have.
class Car:
pass
3. Object
An object is an instance of a class. It represents a real-world entity and can access class methods and
properties.
car1 = Car()
car2 = Car()
4. Constructor (__init__)
A constructor is a special method automatically called when an object is created. It initializes object
attributes.
class Car:
def __init__(self, brand):
[Link] = brand
5. Encapsulation
Encapsulation is the process of hiding internal data and restricting direct access. It ensures controlled
interaction using methods.
class Account:
def __init__(self):
self.__balance = 0
def deposit(self, amount):
self.__balance += amount
6. Inheritance
Inheritance allows one class (child) to acquire properties and methods of another class (parent). This
promotes code reuse.
class Animal:
def speak(self):
print('Animal speaks')
class Dog(Animal):
pass
7. Polymorphism
Polymorphism means 'many forms'. A single function or method can behave differently depending on
the object.
class Bird:
def sound(self):
print('Sound')
class Sparrow(Bird):
def sound(self):
print('Chirp')
8. Abstraction
Abstraction hides implementation details and only shows essential features. It is achieved using
abstract classes and methods.
from abc import ABC, abstractmethod
class Shape(ABC):
@abstractmethod
def area(self):
pass
9. How OOP Works
In OOP, classes define structure, objects use that structure, encapsulation protects data, inheritance
allows reuse, polymorphism provides flexibility, and abstraction simplifies complexity.
10. Practice Exercises
1. Create a class Student with attributes. 2. Implement inheritance with Vehicle and Car. 3. Create
abstract class Shape. 4. Demonstrate polymorphism with animals.