Object Oriented Programming(OOP)
Object Oriented Programming(OOP)
● A car is an object.
● It has attributes like color, brand, and speed.
● It has methods like drive(), brake(), and honk().
Instead of writing everything as raw instructions, OOP structures programs around objects that
interact with each other.
Encapsulation means bundling data (attributes) and methods inside an object and restricting
direct access to some details.
🔹 Example: A car's engine is hidden; you only use the start() function instead of manually
handling internal parts.
python
CopyEdit
class Car:
def __init__(self, brand, color):
self.brand = brand # Public attribute
self.__speed = 0 # Private attribute (hidden from outside)
def get_speed(self):
return self.__speed # Accessing private attribute via a
method
Inheritance allows a class (child) to inherit properties and methods from another class
(parent).
🔹 Example: A Car class can have a subclass ElectricCar that inherits all Car properties but
adds extra features.
python
CopyEdit
class Car:
def __init__(self, brand):
self.brand = brand
def drive(self):
print(f"{self.brand} is driving.")
tesla = ElectricCar("Tesla")
tesla.drive() #✅ Tesla is driving.
tesla.charge() # ✅ Tesla is charging.
Polymorphism allows different objects to use the same method in different ways.
🔹 Example: A Dog and a Cat both have a speak() method, but their behavior is different.
class Animal:
def speak(self):
pass # Defined in subclasses
class Dog(Animal):
def speak(self):
return "Woof!"
class Cat(Animal):
def speak(self):
return "Meow!"
Abstraction means exposing only necessary details and hiding the complexity.
🔹 Example: When using a smartphone, you just tap on apps; you don’t worry about internal
processing.
class Car(Vehicle):
def start(self):
print("Car starts with a key.")
class Bike(Vehicle):
def start(self):
print("Bike starts with a button.")
1. Software Development
✅
OOP makes it easier to develop large-scale applications by organizing code into objects.
Used in: Game development, web applications, enterprise software
2. Web Development
Frameworks like Django (Python) and Spring (Java) use OOP to manage databases, users,
and authentication.
Languages like Java (Android) and Swift (iOS) use OOP to create reusable components for
mobile apps.
4. Game Development
Engines like Unity (C#) and Unreal Engine (C++) use OOP to create game objects like
characters, enemies, and weapons.
Libraries like TensorFlow and PyTorch use OOP to structure models, layers, and training
functions.
Final Thoughts
OOP makes programming organized, reusable, and scalable. It is widely used in modern
software development, from games and mobile apps to AI and web development.