Object Oriented
Object Oriented
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:
self.name = name
self.age = age
self.grade = grade
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:
self.brand = brand
self.model = model
def display_info(self):
# Creating an object
In this example, the Laptop objects with attributes like brand and model. The display_info method
provides information about the laptop.
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
def speak(self):
return "Woof!"
class Cat(Animal):
def speak(self):
cat = Cat()
print(dog.speak())
# Output: Meow!
iv. Polymorphism
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()
```
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
self.balance += amount
self.balance -= amount
return amount
else:
```
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.
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.
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:
Retrieved from
https://www.geeksforgeeks.org/object-oriented-programming-oops-concept-in-python/