Python OOP Simplified

 OPPS(Object-Oriented Programming System)

OOP in Python is a programming style that uses classes to create objects, which contain both data (attributes) and functions (methods).

Why OOP is used in Python?

  • Makes code reusable

  • Makes big programs easy to manage

  • Helps structure code like real-world systems

  • Reduces complexity

  • Improves security and reliability

Main OOP Concepts in Python

  1. Class – Blueprint to create objects

  2. Object – Instance of a class

  3. Inheritance – Reusing code from other classes

  4. Encapsulation – Protecting data

  5. Polymorphism – Same method, different behaviors

  6. Abstraction – Hiding unwanted details

Simple Example

class Student: def show(self): print("This is an OOP class in Python") obj = Student() obj.show()

1. Classes and Objects 

In Python, a class is a blueprint for creating objects. It defines what data (attributes) an object will have and what actions (methods) it can perform. An object is an instance created from a class, meaning it follows the structure the class provides. Classes let you group related data and functions together, making programs organized and easy to manage. To create a class, you use the class keyword. To create an object, you call the class like a function. Understanding classes and objects is essential because they form the foundation of Python’s Object-Oriented Programming.

A class is a blueprint, and an object is an instance created from that blueprint.
Exmaple : 

class Car:
    def __init__(self, brand, color):
        self.brand = brand
        self.color = color

# Creating objects
car1 = Car("Toyota", "Red")
car2 = Car("BMW", "Blue")

print(car1.brand, car1.color)
print(car2.brand, car2.color)

2. Attributes and Methods 

Attributes are variables inside a class that store information about an object, such as a person’s name or age. Methods are functions defined inside a class that describe the behaviors of an object, such as walking or speaking. In Python, the __init__() method initializes object attributes when the object is created. Attributes can be instance attributes, which belong to each object separately, or class attributes, which are shared by all objects of the class. Methods help objects interact with data and perform actions. Understanding attributes and methods helps you build structured programs that behave like real-world systems.

Attributes store data, and methods define behavior in a class.

Example:

class Student: def __init__(self, name, grade): self.name = name # attribute self.grade = grade # attribute def display(self): # method print(f"Name: {self.name}, Grade: {self.grade}") s1 = Student("Alex", "A") s1.display()

3. Inheritance 

Inheritance allows one class (child class) to take properties and behaviors from another class (parent class). This helps avoid repeating code. For example, you can create a general Animal class and then create Dog and Cat classes that inherit from it. The child class can use parent methods or replace them with new ones by overriding. Python supports single inheritance (one parent class) and multiple inheritance (more than one parent). Inheritance is powerful because it promotes code reuse and makes programs easier to expand. It also helps create cleaner and more organized class structures.

A child class inherits attributes and methods from a parent class.

Example:

class Animal: def sound(self): print("Animals make sounds") class Dog(Animal): # Inherits from Animal def sound(self): # Method overriding print("Dog barks") d = Dog() d.sound()

4. Encapsulation

Encapsulation is the practice of keeping data safe by restricting direct access to it. In Python, this is done by using public, protected, and private attributes. A public attribute can be accessed anywhere, a protected attribute (using a single underscore) suggests limited access, and a private attribute (double underscore) hides the data from outside the class. Methods called getters and setters are used to safely access or update private data. Encapsulation helps prevent accidental changes and keeps the internal workings of a class hidden. This improves security, reduces errors, and keeps code clean and easy to maintain.

Encapsulation protects data by making attributes private.

Example:

class BankAccount: def __init__(self, balance): self.__balance = balance # private attribute def deposit(self, amount): self.__balance += amount def get_balance(self): # getter method return self.__balance acc = BankAccount(1000) acc.deposit(500) print(acc.get_balance())

5. Polymorphism 

Polymorphism allows different classes to use the same method name but perform different actions. This makes code flexible and easy to extend. For example, both Dog and Bird classes can have a sound() method, but one returns "bark" while the other returns "chirp." Python also supports polymorphism through method overriding, where a child class replaces a parent class’s method with its own version. Polymorphism is useful when writing functions that work with many types of objects. It helps simplify code and allows developers to design systems where multiple classes share similar behaviors in their own unique ways.

Different classes can have the same method name but different behaviors.

Example:

class Bird: def sound(self): print("Bird chirps") class Cat: def sound(self): print("Cat meows") for animal in (Bird(), Cat()): animal.sound() # Same method name, different output

Post a Comment

If you have any doubts, please tell me know

Previous Post Next Post