OOPs Concept
OOPs Concept
Object-Oriented Programming (OOP) is a programming paradigm that organizes code into objects,
which are instances of classes. OOP helps in designing modular, reusable, and maintainable code.
1. Concepts of OOP
1.1 Object
An object is a real-world entity that has state (data/attributes) and behavior (functions/methods).
1.2 Class
A class is a blueprint or template for creating objects. It defines the attributes (variables) and
methods (functions) that its objects will have.
Example: Think of a car manufacturing blueprint. It defines how a car should be built, but an
actual car is an object made from that blueprint.
Python Example:
class Car:
Encapsulation means wrapping data and methods within a class and restricting direct access to
some details of the object.
Why?
Example: In a bank account system, the balance should not be directly accessible or modified.
Instead, it should be accessed through methods.
class BankAccount:
self.__balance += amount
print(f"Deposited: {amount}")
def get_balance(self):
return self.__balance
# Creating an object
account = BankAccount(1000)
account.deposit(500)
# print(account.__balance) # AttributeError
2.2 Abstraction (Hiding Complexity)
Abstraction means hiding the implementation details and only exposing the necessary
functionalities.
Why?
• Reduces complexity
Example: When you use an ATM, you don’t need to know how it processes transactions
internally. You just insert your card, enter a PIN, and withdraw money.
@abstractmethod
def start_engine(self):
class Car(Vehicle):
def start_engine(self):
# Creating object
my_car = Car()
Here, Vehicle is an abstract class that defines a general structure. Car implements the method.
2.3 Inheritance (Code Reusability)
Inheritance allows one class (child/subclass) to inherit the properties and behaviors of another
class (parent/superclass).
Why?
Example:
A Car is a type of Vehicle, so we can create a general Vehicle class and let Car inherit from it.
class Vehicle:
self.brand = brand
def drive(self):
print(f"{self.brand} is moving.")
class Car(Vehicle):
def honk(self):
my_car = Car("Honda")
Here, Car inherits the drive() method from Vehicle, avoiding redundant code.
2.4 Polymorphism (Multiple Forms)
Polymorphism allows the same method or function to behave differently based on the object
calling it.
Why?
• Increases flexibility
class Animal:
def make_sound(self):
class Dog(Animal):
print("Dog barks")
# Creating objects
animal = Animal()
dog = Dog()
Here, the make_sound() method behaves differently for Animal and Dog.
Method Overloading (Same method, different parameters)
Python doesn’t support method overloading directly, but it can be achieved using default
parameters:
class MathOperations:
return a + b + c
math = MathOperations()
A special method in Python classes that runs automatically when an object is created.
class Person:
self.name = name
self.age = age
def __init__(self):
print("Object Created.")
def __del__(self):
print("Object Destroyed.")
obj = Example()
A method that belongs to a class but does not access instance attributes.
class Math:
@staticmethod
def square(x):
return x * x
print(Math.square(4)) # Output: 16
4. Advantages of OOP
5. Conclusion
OOP is a powerful approach that makes coding modular, reusable, and efficient. It is widely used
in languages like Python, Java, C++, C#, and JavaScript to build complex applications like
games, web apps, and enterprise software.