OOP in Simple Words (Step 12)
OOP in Simple Words (Step 12)
1. What Is OOP?
Definition: OOP (Object-Oriented Programming) is a way to write code by organizing it
into "objects." Objects are like real-world things (e.g., a car, a person) that have
properties (like color, name) and behaviors (like driving, talking). OOP makes code
easier to manage, reuse, and understand.
Example:
Let’s create a simple "Dog" object.
3. What is a Class?
Definition: A class is a blueprint for creating objects. It defines what properties (data)
and behaviors (functions) the object will have.
Example:
We already saw a Dog class above. Here’s another example with a Car class.
Page 1 of 13
Sahir Ahmed Sheikh
COMPREHENSIVE GUIDE TO OOP IN PYTHON
4. What is an Object?
Definition: An object is something you create from a class. It’s like using a blueprint
(class) to build an actual thing (object).
Example:
In the above code, my_car is an object of the Car class.
Example:
Let’s add more attributes and methods to a Student class.
6. Instantiating Objects
Definition: Instantiating means creating an object from a class. You use the class name
with parentheses, like Student(“Ali”, 20).
Example:
We’ve already instantiated objects like my_dog, my_car, and student1 in the examples
above.
Example:
In the Student class, self.name means "the name of this student object."
Page 2 of 13
Sahir Ahmed Sheikh
COMPREHENSIVE GUIDE TO OOP IN PYTHON
8. What is a Constructor?
Definition: A constructor is a special method called __init__ that runs automatically
when you create an object. It’s used to set up the object’s initial attributes.
Example:
The __init__ method in all the examples above is a constructor.
9. Parameterized Constructors
Definition: A constructor that takes parameters (like name, age) to initialize the
object’s attributes.
Example:
The Student class above has a parameterized constructor: def __init__(self, name, age).
Example:
Page 3 of 13
Sahir Ahmed Sheikh
COMPREHENSIVE GUIDE TO OOP IN PYTHON
Example:
Page 4 of 13
Sahir Ahmed Sheikh
COMPREHENSIVE GUIDE TO OOP IN PYTHON
1. Encapsulation
It refers to wrapping up data and methods into a single unit (a class) and restricting
direct access to some of an object's components.
Think of it like: A capsule that contains both medicine (data) and instructions
(methods) inside it — you can’t directly access what's inside; you follow the provided
interface (e.g., a method) to use it.
Access Modifiers: Public (normal), Private (with __ ), Protected (with _ ).
Page 5 of 13
Sahir Ahmed Sheikh
COMPREHENSIVE GUIDE TO OOP IN PYTHON
Example:
Example:
We already did this in the BankAccount example above.
Example:
Page 6 of 13
Sahir Ahmed Sheikh
COMPREHENSIVE GUIDE TO OOP IN PYTHON
2. Inheritance
Definition: Inheritance lets a class (child) take properties and methods from another
class (parent).
Types Of Inheritance:
Single Inheritance: One parent, one child.
Multi-level Inheritance: A chain (Parent → Child → Grandchild).
Multiple Inheritance: One child with multiple parents.
Page 7 of 13
Sahir Ahmed Sheikh
COMPREHENSIVE GUIDE TO OOP IN PYTHON
Example:
3. Polymorphism
Definition: Polymorphism means “many forms”. It lets different classes have methods
with the same name but different behaviors.
Types Of Polymorphism In Python
Method Overriding: Child class changes a parent’s method.
Operator Overloading: Changing how operators (like +) work.
Duck Typing: If an object behaves like a type, it’s treated as that type.
Page 8 of 13
Sahir Ahmed Sheikh
COMPREHENSIVE GUIDE TO OOP IN PYTHON
Page 9 of 13
Sahir Ahmed Sheikh
COMPREHENSIVE GUIDE TO OOP IN PYTHON
4. Abstraction
Definition: Abstraction hides complex details and shows only the necessary parts. In
Python, you use abstract classes to define methods that must be implemented by child
classes.
Key Components:
ABC: Abstract Base Class (from the abc module) makes a class abstract.
@abstractmethod : Marks a method as abstract (must be implemented by
child classes).
Child Classes: Must implement all abstract methods.
Example:
Page 10 of 13
Sahir Ahmed Sheikh
COMPREHENSIVE GUIDE TO OOP IN PYTHON
This means your class inherits some built-in behaviors from the object class (e.g., how
it’s compared, how it looks when printed, etc.).
All of these are objects, and inherit from the root object class.
Benefit Explanation
Introspection You can examine any object at runtime using type(), dir(), etc.
Page 11 of 13
Sahir Ahmed Sheikh
COMPREHENSIVE GUIDE TO OOP IN PYTHON
Real-World Example:
This loop works seamlessly because everything is an object and can be handled uniformly.
Developer-focused string
__repr__ repr(obj)
representation
__lt__, __gt__ Less than / Greater than obj1 < obj2, obj1 > obj2
Page 12 of 13
Sahir Ahmed Sheikh
COMPREHENSIVE GUIDE TO OOP IN PYTHON
Example:
Page 13 of 13