✍️Introduction
OOP (Object-Oriented Programming) is a programming approach that uses objects and classes.
Python fully supports OOP, which helps in writing clean, reusable, and scalable code.
Object-Oriented Programming (OOP) organizes programs into objects that contain both data and functions.
It helps model real-world entities like students, cars, or accounts in the form of classes and objects.
OOP makes programs easier to manage by improving code reusability, modularity, and maintainability, which is why it is widely used in large software projects.
What is OOP?
-
Objects → real-world entities
-
Classes → blueprint of objects
Classes and objects are the two core concepts in object-oriented programming.
Example:
Class = Student
Object = Rutuja , Tushar
1️⃣ Class in Python :
A class in Python is a blueprint used to create objects with similar properties and behaviors.
class keyword followed by the class name.A class contains variables (attributes) and functions (methods) that define the characteristics of objects created from it.
Syntax for ceating class:
Example:
2️⃣ Object in Python :
When a class is created, no memory is allocated until an object is created.
An object allows us to access the variables and methods of a class.
We can create multiple objects from one class, and each object can have different values.
Example:
3️⃣__init__() Constructor in Python :
A constructor is a special method in a class that is automatically called when an object is created. In Python, the constructor is written as __init__() and is used to initialize object data (attributes).
- The
__init__()method runs automatically when a new object is created.
-
It helps assign initial values to object variables.
-
The
selfparameter refers to the current object. Constructors make it easy to set up objects with different values.
Example:
class Student:
4️⃣ self Keyword :
self is a reference to the current object of the class. It is used to access the variables and methods of the class.selfrepresents the current instance (object).-
It is used inside class methods to access object data.
-
Every method in a class must have
selfas the first parameter. -
It is not a keyword, but a commonly used convention.
Example:
5️⃣ Methods in Class :
Methods are functions that are defined inside a class and are used to perform operations on objects.
Methods define the behavior of objects.
-
They are written inside a class.
-
Methods usually use
selfto access object data.
-
They are called using objects of the class.
Example:
6️⃣ Four Pillars of OOP :
The four pillars are:
-
Encapsulation
Encapsulation means wrapping data (variables) and methods (functions) together inside a class.
It helps in protecting data from direct access and keeps it safe. -
Inheritance
Inheritance allows one class to use the properties and methods of another class.
It helps in reusing code and reducing duplication. -
Polymorphism
Polymorphism means "many forms".
It allows the same function or method name to behave differently in different situations. -
Abstraction
Abstraction means hiding unnecessary details and showing only important features.
It helps in reducing complexity and makes code easier to understand.
In short:
The four pillars of OOP make programs secure, reusable, flexible, and easy to maintain.
Encapsulation means binding data (variables) and methods (functions) together inside a class.
It is used to protect data and prevent direct access from outside the class.
Encapsulation helps in keeping the data safe and controlled by allowing access only through methods.
Example:
✔ Improves security
✔ Data protection
✔ Easy data management
Note:Encapsulation controls how data is accessed and modified.
🔹 2. Inheritance :
Inheritance is a feature of OOP where one class (child class) inherits properties and methods from another class (parent class).
It allows code to be reused and helps in creating a relationship between classes.
The child class can use the parent class methods without rewriting the code.
Example:
🔹 3. Polymorphism :
Polymorphism means “many forms.”
In OOP, polymorphism allows the same method name to behave differently depending on the object.
It is commonly achieved using method overriding or method overloading (Python mainly uses overriding).
Polymorphism makes code flexible and easier to extend because the same function name can work in different ways.
Example:
🔹 4. Abstraction :
Abstraction means hiding the internal implementation details and showing only the important features to the user.
In Python, abstraction is implemented using abstract classes and abstract methods with the help of the abc module.
Abstraction helps in reducing complexity and increases security because the user only sees what is necessary.
✔ Hides unnecessary details
✔ Shows essential features only
✔ Improves code security and structure
Example:
7️⃣ Access Modifiers in Python :
They help in protecting data and controlling how class members are accessed.
Python does not have strict access control like some other languages, but it uses naming conventions to indicate access levels.
Access modifiers are mainly used for:
-
Data protection
-
Encapsulation
-
Controlling access to variables
-
Improving code security
| Modifier | Syntax | Meaning |
|---|---|---|
| Public | name | Accessible everywhere |
| Protected | _name | Within class & subclass |
| Private | __name | Only within class |
🔹1. Public :
Public members can be accessed from anywhere inside or outside the class.
Example:
class Student:
name = "Rutuja" # Public variable
s = Student()
print(s.name) # AccessibleOutput:Rutuja"Code executed successfully"
🔹2. Protected :
Protected members are intended to be used within the class and its subclasses.
(They can still be accessed outside, but it is not recommended.)
Example:
class Student:
_age = 20 # Protected variable
s = Student()
print(s._age) # Possible but not recommendedOutput:20"Code executed successfully"
🔹3. Private :
Private members are accessible only inside the class.
They cannot be accessed directly outside the class.
Example:
class Student: __marks = 90 # Private variable def show_marks(self): print("Marks:", self.__marks) # Create object s1 = Student() # Access private variable using method s1.show_marks()Output:Marks: 90"Code executed successfully"Accessing Private Variable using Name Mangling
print(s1._Student__marks)Output
90Explanation:
__marksis a private variable.
It cannot be accessed directly like
s1.__marks.It can be accessed using a class method or
_Student__marks(name mangling).
📝 Note:
-
Public → No underscore
-
Protected → Single underscore
_ -
Private → Double underscore
__ -
Private variables can only be accessed using class methods.
8️⃣ Real-Life OOP Example :
9️⃣ Why Use OOP?
-
Better code structure
-
Easy maintenance
-
Reusability
-
Real-world modeling
❌ Common OOP Mistakes:
-
Forgetting
self -
Wrong indentation
-
Not using constructor properly
Conclusion :
Python OOP makes programs modular, readable, and powerful.
Understanding OOP is essential for real-world applications, interviews, and projects.
💬 Interview Question :
Explain Inheritance vs Polymorphism in your own words ?
Comment below 👇😊
📌 Related Articles :
Comments
Post a Comment