Object-Oriented Programming (OOPs)
Learn how Python uses classes and objects to organize programs into reusable, structured and maintainable code.
What is OOPs?
OOPs (Object-Oriented Programming) is a programming approach where a program is designed using objects and classes.
An object represents a real-world entity such as a student, employee, course or bank account. A class acts as a blueprint from which objects are created.
Python supports Object-Oriented Programming and provides important concepts such as:
- Classes and Objects
- Constructors
- Encapsulation
- Inheritance
- Polymorphism
- Abstraction
Why Do We Use OOPs?
OOPs helps developers build large applications in a structured and reusable way.
| Benefit | Description |
|---|---|
| Reusability | Existing classes and methods can be reused. |
| Maintainability | Code can be organized into smaller components. |
| Security | Data can be controlled using encapsulation. |
| Scalability | Applications can be expanded more easily. |
| Code Organization | Related data and behavior can be grouped together. |
OOPs Concept Diagram
The following diagram shows the major concepts of Object-Oriented Programming in Python.
Classes & Objects
Class is a blueprint and object is an instance created from that blueprint.
Encapsulation
Combines data and methods together and controls access to data.
Inheritance
Allows a child class to reuse properties and methods of a parent class.
Polymorphism
Allows the same method or interface to behave differently in different objects.
Abstraction
Hides unnecessary implementation details and exposes essential functionality.
Constructors
Used to initialize an object when it is created.
Example 🤓👀
Consider a training institute such as CIIT Training Institute.
We can represent a student using an object. The student can have data such as name, course and fees, along with behaviors such as displaying details.
class Student:
def __init__(self, name, course, fees):
self.name = name
self.course = course
self.fees = fees
def display_details(self):
print("Name:", self.name)
print("Course:", self.course)
print("Fees:", self.fees)
student1 = Student("Sam", "Java Full Stack", 35000)
student1.display_details()
Here, Student is the class and student1 is an object of that class.
Output
Name: Sam
Course: Java Full Stack
Fees: 35000
Class and Object
A class defines the structure and behavior of an entity, while an object is an actual instance of that class.
class Student:
pass
student1 = Student()
student2 = Student()
In this example, Student is the class and student1 and student2 are objects.
Attributes and Methods
Objects generally contain two important types of members:
- Attributes: Store object data.
- Methods: Define actions or behavior.
class Student:
def display(self):
print("Student details")
student = Student()
student.display()
What is self?
In Python, self refers to the current object.
It is used to access the attributes and methods of that
particular object.
class Student:
def __init__(self, name):
self.name = name
def show(self):
print(self.name)
student = Student("Amit")
student.show()
Here, self.name stores the name inside the
current student object.
Four Important OOPs Concepts
| Concept | Meaning |
|---|---|
| Encapsulation | Bundling data and methods together and controlling access. |
| Inheritance | Reusing features of an existing class. |
| Polymorphism | Same interface or method with different behavior. |
| Abstraction | Showing essential functionality while hiding implementation details. |
OOPs Program Flow
Class
↓
Define Attributes & Methods
↓
Create Object
↓
Object Stores Data
↓
Object Calls Methods
↓
Program Performs Required Operation
CIIT Example 📩🖥️
Suppose we want to manage students enrolled in different courses.
class Student:
def __init__(self, name, course):
self.name = name
self.course = course
def show_course(self):
print(self.name, "is learning", self.course)
student1 = Student("Priya", "Python Full Stack")
student2 = Student("Amit", ".NET Full Stack")
student1.show_course()
student2.show_course()
This approach keeps student data and student-related behavior together inside the Student class.
Output
Priya is learning Python Full Stack
Amit is learning .NET Full Stack
Important Points to Remember
- Python supports Object-Oriented Programming.
- A class is a blueprint for creating objects.
- An object is an instance of a class.
- Attributes store data.
- Methods define behavior.
-
selfrefers to the current object. -
The
__init__()method is commonly used for object initialization. - OOP concepts help create reusable and maintainable code.
Common Beginner Mistakes
-
Forgetting the colon
:after class definition. - Incorrect indentation inside the class.
-
Forgetting
selfin instance methods. - Confusing a class with an object.
- Accessing an attribute before assigning it.
Interview Points
- What is OOPs?
- What is a class?
- What is an object?
- What is the difference between class and object?
- What is the purpose of self in Python?
- What is a constructor?
- Explain encapsulation, inheritance, polymorphism and abstraction.
CIIT Learning Point
OOPs is one of the most important concepts for Python developers. Understanding classes, objects and the four major OOP concepts will help you build structured real-world applications.
CIIT Practice Tasks
-
Create a
Studentclass with name and age. -
Create an
Employeeclass with name, department and salary. -
Create a
Courseclass with course name and fees. - Create multiple objects from the same class.
- Create a method that displays object information.
Summary :
OOPs (Object-Oriented Programming) is a programming approach based on classes and objects. A class acts as a blueprint and an object is an instance of that class. Python OOPs includes important concepts such as constructors, encapsulation, inheritance, polymorphism and abstraction. Understanding these concepts helps developers create reusable, organized and maintainable applications.