What Is OOP?
What Is OOP?
What is OOP?
• Object-Oriented Programming (OOP) is a programming paradigm
based on the concept of “objects”, which bundle data and behavior
together.
• Instead of writing procedures or functions, OOP helps you model real-
world entities using classes and objects.
• Class
• A class is a blueprint for creating objects. It defines attributes (data)
and methods (functions) that describe the behavior of the objects.
• Object
• An object is an instance of a class. It is a real-world entity created
from the class, with its own unique data.
• 4 Pillars of OOP in Python
• 1. EncapsulationHides internal state and protects data using
access control
• 2. Inheritance One class can inherit attributes and methods
from another
• 3. Polymorphism Different classes can define methods
with the same name but different behavior
• 4. Abstraction Hides complex implementation and shows
only essential features
Example;
• class Car: • my_car = Car("Toyota")
• def __init__(self, brand): • my_car.drive()
• self.brand = brand • # Output: Toyota is driving.
• def drive(self):
• print(f"{self.brand} is
driving.")
Book class with two objects
• class Book: • # Create two book objects
• def __init__(self, title, author, • book1 = Book("The Great
pages): Gatsby", "F. Scott Fitzgerald",
• self.title = title 180)
• self.author = author • book2 = Book("1984", "George
• self.pages = pages Orwell", 328)
• make_sound(d) # Woof!
• make_sound(c) # Meow!
Types of Polymorphism in Python
• Method Overriding Same • print(5 + 10) # 15 (int
method name in child class addition)
overrides parent method • print("Hello " + "World") #
• Operator Overloading Same Hello World (string
operator does different things concatenation)
depending on types (e.g. + works • Here, + works differently
for both numbers and strings) depending on the data type →
that's polymorphism too.