Object Oriented Programming (OOP)
Object-Oriented Programming (OOP) is a programming paradigm that organizes software design around
objects rather than actions and data rather than logic. In OOP, objects are instances of classes, which
represent real-world entities. This paradigm allows for the modular design of complex systems by
breaking them down into smaller, more manageable parts.
i. Object
In OOP, an object is a particular instance of a class where the object can be a combination of variables,
functions, and data structures. Objects are the basic building blocks of OOP and encapsulate both data
(attributes) and behavior (methods).
Example in Python:
class Student:
def __init__(self, name, age, grade):
self.name = name
self.age = age
self.grade = grade
# Creating instances (objects) of the Student class
student1 = Student("Alice", 18, "12th")
student2 = Student("Bob", 17, "11th")
In this example, each student object represents a student with attributes such as name, age, and grade.
ii. Class
A class in OOP is a blueprint for creating objects. It defines the attributes and methods that all objects of
that class will have. Classes act as templates for creating objects with similar properties and behaviors.
Example in Python:
class Laptop:
def __init__(self, brand, model):
self.brand = brand
self.model = model
def display_info(self):
print(f"This laptop is a {self.brand} {self.model}")
# Creating an object
laptop1 = Laptop("Apple", "MacBook Pro")
laptop1.display_info() # Output: This laptop is a Apple MacBook Pro
In this example, the Laptop objects with attributes like brand and model. The display_info method
provides information about the laptop.
iii. Inheritance and Code Re-use
Inheritance is a fundamental concept in OOP that allows a new class (derived class or subclass) to inherit
attributes and methods from an existing class (base class or superclass). This promotes code reusability
and helps in creating a hierarchy of classes.
Example in Python:
# Base class class Animal:
def speak(self):
pass
# Derived classes class Dog(Animal):
def speak(self):
return "Woof!"
class Cat(Animal):
def speak(self):
return "Meow!" dog = Dog()
cat = Cat()
print(dog.speak())
# Output: Woof! print(cat.speak())
# Output: Meow!
iv. Polymorphism
Polymorphism allows objects of different classes to be treated as objects of a common superclass. It
enables flexibility in programming by allowing functions to work with objects of multiple types and
classes.
Example in Python:
class Dog(Animal):
def speak(self):
return "Woof!"
class Cat(Animal):
def speak(self):
return "Meow!"
# Polymorphism in action
def make_sound(animal):
return animal.speak()
dog = Dog()
cat = Cat()
print(make_sound(dog)) # Output: Woof!
print(make_sound(cat)) # Output: Meow!
```
v. Encapsulation
Encapsulation is the bundling of data (attributes) and methods that operate on the data into a single
unit called an object. It restricts direct access to some components of an object, promoting data security
and hiding the internal state of an object.
Example in Python:
class BankAccount:
def init(self):
self.balance = 0
def deposit(self, amount):
self.balance += amount
def withdraw(self, amount):
if self.balance >= amount:
self.balance -= amount
return amount
else:
return "Insufficient funds"
```
Advantages of using OOP:
Code Reusability: Classes and objects promote code reuse, reducing redundancy and improving
efficiency.
Modularity: OOP promotes modularity by dividing complex problems into smaller, more manageable
parts.
Flexibility: OOP allows for easy modification and extension of existing code.
Data Security: Encapsulation helps in protecting data within objects.
Maintainability: OOP code is easier to maintain and debug due to its organized structure.
Polymorphism: Polymorphism enables flexibility and extensibility by allowing objects of different classes
to be treated uniformly.
Drawbacks of using OOP:
Complexity: OOP can introduce complexity, especially for beginners or when dealing with large projects.
Performance Overhead: OOP can have performance overhead due to dynamic method dispatch and
other features.
Learning Curve: Understanding OOP concepts thoroughly may require more time compared to
procedural programming.
Potential for Tight Coupling: Improper design or implementation of OOP principles can lead to tight
coupling between classes, making the codebase more difficult to maintain and refactor.
Reference:
GeeksforGeeks. (n.d.). Object Oriented Programming (OOPs) Concept in Python.
Retrieved from
https://www.geeksforgeeks.org/object-oriented-programming-oops-concept-in-python/