What is OOPS (Object-Oriented Programming)? || Python

1. What is OOPS ( Object-Oriented Programming )? 

OOPS stands for Object-Oriented Programming System. It is a programming approach where we organize code using objects and classes instead of writing everything as functions. An object represents a real-world entity like a car, student, or bank account. OOPS helps make programs easy to understand, reusable, secure, and maintainable. Python supports OOPS very well and is widely used in software development, web applications, and data science.

Example :
                  
class Student:
    name = "Kamal"

obj = Student()
print(obj.name)


2. Class in Python

A class is a blueprint or template used to create objects. It defines properties (variables) and methods (functions) that describe an object. For example, a “Car” class can have properties like color and speed, and methods like start and stop. Classes help reduce code repetition and improve structure. In Python, we create a class using the class keyword.

Example :

class Car:
    brand = "Toyota"
    color = "Red"
my_car = Car()
print(my_car.brand)


3. Object in Python

An object is an instance of a class. It represents a real thing created using the class blueprint. When a class is defined, no memory is used until an object is created. Objects allow us to access class variables and methods. Multiple objects can be created from the same class, each having different values.

Example:

class Mobile:
    model = "Samsung"
phone1 = Mobile()
print(phone1.model)


4. Constructor (__init__ Method)

A constructor is a special method that runs automatically when an object is created. In Python, it is called __init__. It is used to initialize object data. Constructors make classes more dynamic because values can be passed while creating objects. This helps store different data for each object.

Example:

class Person:

    def __init__(self, name, age):

        self.name = name&

        self.age = age

p1 = Person("Kamal", 22)

print(p1.name, p1.age)


5. Inheritance

Inheritance allows one class to use properties and methods of another class. The class that inherits is called the child class, and the class being inherited is the parent class. Inheritance helps reduce code duplication and improves reusability. It is useful when classes have a common relationship.

Example:

class Animal: def sound(self): print("Animal makes sound") class Dog(Animal): pass d = Dog() d.sound()


6. Encapsulation

Encapsulation means wrapping data and methods together and restricting direct access to data.

It helps protect data from unauthorized modification. In Python, encapsulation is achieved using

private variables with double underscores (__). This improves security and data control.


Example:

class Account:
def __init__(self):
self.__balance = 5000
def show_balance(self):
print(self.__balance)
acc = Account()
acc.show_balance()



7. Polymorphism


Polymorphism means “many forms”. It allows the same method name to behave differently depending on the object.
Polymorphism improves flexibility and code readability. In Python, polymorphism can be achieved through
method overriding and functions.

Example:

class Bird:
def fly(self):
print("Bird can fly")

class Penguin(Bird):
def fly(self):
print("Penguin cannot fly")

b = Penguin()
b.fly()


8. Abstraction


Abstraction hides unnecessary details and shows only essential features to the user.
It helps reduce complexity. In Python, abstraction is implemented using abstract
classes and the abc module. It is useful when you want to enforce rules in
child classes.

Example:

from abc import ABC, abstractmethod class Vehicle(ABC): @abstractmethod def start(self): pass class Bike(Vehicle): def start(self): print("Bike started") b = Bike() b.start()


Summary

OOPS in Python makes code clean, reusable, secure, and easy to manage.
Key concepts are Class, Object, Inheritance, Encapsulation, Polymorphism, and Abstraction.









Post a Comment

If you have any doubts, please tell me know

Previous Post Next Post