0% found this document useful (0 votes)
2 views

Object Oriented Programming(OOP)

Uploaded by

4eri4l
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Object Oriented Programming(OOP)

Uploaded by

4eri4l
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

What is Object-Oriented Programming (OOP)?

Object-Oriented Programming (OOP) is a programming paradigm that organizes code using


objects. These objects represent real-world entities and contain data (attributes) and behavior
(methods).

Think of it like this:

●​ 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.

Key Principles of OOP


There are four main principles of OOP:

1. Encapsulation (Data Hiding)

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 accelerate(self, value):


self.__speed += value # Modifying private attribute inside
the class

def get_speed(self):
return self.__speed # Accessing private attribute via a
method

car = Car("Toyota", "Red")


car.accelerate(30)
print(car.get_speed()) # tput: 30

2. Inheritance (Code Reusability)

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.")

class ElectricCar(Car): # Inheriting from Car


def charge(self):
print(f"{self.brand} is charging.")

tesla = ElectricCar("Tesla")
tesla.drive() #✅ Tesla is driving.
tesla.charge() # ✅ Tesla is charging.

3. Polymorphism (Multiple Forms)

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!"

animals = [Dog(), Cat()]


for animal in animals:
print(animal.speak()) # ✅ Woof! ✅ Meow!

4. Abstraction (Hiding Complexity)

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.

from abc import ABC, abstractmethod

class Vehicle(ABC): # Abstract class


@abstractmethod
def start(self):
pass

class Car(Vehicle):
def start(self):
print("Car starts with a key.")

class Bike(Vehicle):
def start(self):
print("Bike starts with a button.")

vehicles = [Car(), Bike()]


for v in vehicles:
v.start() # ✅ Car starts with a key. ✅ Bike starts with a
button.

Uses of Object-Oriented Programming


OOP is used in various fields to create efficient, scalable, and maintainable software.

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.

3. Mobile App Development

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.

5. Artificial Intelligence & Machine Learning

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.

Would you like to try writing some OOP code yourself? 🚀

You might also like