OOPs in Python - Cheat Sheet
1. What is OOP?
Object-Oriented Programming is a method of structuring a program by bundling related properties and behaviors into
individual objects.
2. Four Pillars of OOP
- Encapsulation: Hiding data within class.
- Abstraction: Showing essential features, hiding internal details.
- Inheritance: Acquiring properties from parent class.
- Polymorphism: Multiple forms via overriding/overloading.
3. Class and Object
class Student:
def __init__(self, name, age):
self.name = name
self.age = age
def show(self):
print(self.name, self.age)
s1 = Student('Jassu', 21)
4. Encapsulation
class Account:
def __init__(self, balance):
self.__balance = balance
def deposit(self, amount):
self.__balance += amount
5. Abstraction
from abc import ABC, abstractmethod
class Shape(ABC):
@abstractmethod
def area(self): pass
6. Inheritance
class Animal:
def sound(self): print('Animal')
OOPs in Python - Cheat Sheet
class Dog(Animal):
def sound(self): print('Bark')
7. Polymorphism
class A:
def show(self): print('A')
class B(A):
def show(self): print('B')
8. Constructor (__init__)
Called automatically when object is created.
9. super()
Used to call parent class methods inside child class.
10. Types of Methods
- Instance Method
- Class Method (@classmethod)
- Static Method (@staticmethod)
11. Operator Overloading
class Point:
def __add__(self, other):
return self.x + other.x
12. Access Modifiers
- Public: self.var
- Protected: self._var
- Private: self.__var