_Object-Oriented Programming (OOP)
_Object-Oriented Programming (OOP)
1. Modularity: Breaking down a program into manageable and reusable parts.
2. Abstraction: Hiding complex implementation details and exposing only the necessary
features.
3. Reusability: Using existing code across different programs to reduce redundancy.
4. Scalability and Maintenance: Making code easier to modify and extend over time.
● Class: A blueprint for creating objects. It defines the properties and behaviors that its
objects will have. For example, a Car class might have attributes like color and model
and methods like start() and stop().
● Object: An instance of a class. For example, a specific car like a red Toyota Corolla is an
object of the Car class.
class Car:
def __init__(self, color, model):
self.color = color
self.model = model
def start(self):
print(f"The {self.color} {self.model} is starting.")
# Creating an object
my_car = Car("Red", "Toyota Corolla")
my_car.start()
2. Encapsulation
● Encapsulation involves bundling the data (attributes) and methods (functions) that
operate on the data into a single unit (class) and restricting direct access to some
components. This is achieved using access modifiers:
○ Public: Accessible from anywhere.
○ Private: Accessible only within the class (denoted by __ prefix in Python).
○ Protected: Accessible within the class and subclasses (denoted by _ prefix in
Python).
class BankAccount:
def __init__(self, balance):
self.__balance = balance # Private attribute
def get_balance(self):
return self.__balance
account = BankAccount(1000)
account.deposit(500)
print(account.get_balance())
3. Inheritance
● Inheritance allows a class (child class) to acquire the properties and behaviors of
another class (parent class). This promotes code reuse.
● The child class can add new properties or override existing ones.
class Vehicle:
def __init__(self, brand):
self.brand = brand
def move(self):
print("The vehicle is moving.")
class Car(Vehicle):
def move(self):
print(f"The {self.brand} car is driving.")
my_car = Car("Toyota")
my_car.move()
4. Polymorphism
● Polymorphism means "many forms." It allows methods to have the same name but
behave differently based on the object calling them.
● This is achieved through method overriding (redefining a method in a subclass) or
method overloading (defining multiple methods with the same name but different
parameters).
class Animal:
def speak(self):
print("The animal makes a sound.")
class Dog(Animal):
def speak(self):
print("The dog barks.")
animal = Animal()
dog = Dog()
animal.speak()
dog.speak()
Different programming languages implement OOP features in unique ways. Below are the
general structures you’ll encounter in OOP languages:
1. Class Declaration
Defines the structure and behavior of objects. Most OOP languages use a class keyword.
class ClassName:
def __init__(self):
pass
2. Object Instantiation
object_name = ClassName()
Attributes are variables defined within a class, while methods are functions associated with an
object.
class Example:
def __init__(self, value):
self.value = value
def display(self):
print(self.value)
obj = Example(10)
obj.display()
4. Access Modifiers
5. Inheritance Syntax
Supports reusing code by extending classes. Most languages use a colon (:) or keyword like
extends.
class Parent:
pass
class Child(Parent):
pass
● Abstract classes are base classes that cannot be instantiated and may include abstract
methods (methods without implementation).
● Interfaces define a contract for classes to implement.
class AbstractClass(ABC):
@abstractmethod
def abstract_method(self):
pass
class ConcreteClass(AbstractClass):
def abstract_method(self):
print("Implementation")