OPPS(Object-Oriented Programming System)
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
-
Class – Blueprint to create objects
-
Object – Instance of a class
-
Inheritance – Reusing code from other classes
-
Encapsulation – Protecting data
-
Polymorphism – Same method, different behaviors
-
Abstraction – Hiding unwanted details
Simple Example
1. Classes and Objects
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.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:
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:
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:
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:
Post a Comment
If you have any doubts, please tell me know