Object-Oriented Programming (OOP) in Python
In the previous lesson, we learned about Modules and Packages and how they help organize and reuse Python code efficiently. Today, we will learn about Object-Oriented Programming (OOP), one of the most powerful programming approaches in Python.
Object-Oriented Programming helps programmers structure applications using objects and classes, making programs more organized, reusable, and scalable.
What is Object-Oriented Programming?
Object-Oriented Programming (OOP) is a programming approach that organizes code using:
- Classes
- Objects
OOP models real-world entities such as:
- Students
- Cars
- Employees
- Bank Accounts
Each object contains:
- Attributes (Data)
- Methods (Functions)
Why OOP is Important
OOP helps programmers:
- Organize code properly
- Reuse code efficiently
- Build scalable applications
- Improve security
- Simplify maintenance
- Represent real-world systems
Most modern software applications use Object-Oriented Programming.
What is a Class?
A Class is a blueprint for creating objects.
It defines:
- Variables
- Functions
- Behaviors
Syntax
class ClassName:
# class body
Creating a Class
Example
class Student:
pass
This creates an empty class named "Student".
What is an Object?
An Object is an instance of a class.
Objects use the structure defined inside the class.
Example
class Student:
pass
student1 = Student()
print(student1)
The init() Method
The "init()" method is called automatically when an object is created.
It initializes object attributes.
Example
class Student:
def __init__(self, name, age):
self.name = name
self.age = age
Creating Objects with Attributes
Example
class Student:
def __init__(self, name, age):
self.name = name
self.age = age
student1 = Student("Augustine", 21)
print(student1.name)
print(student1.age)
Output
Augustine
21
Understanding self
The "self" keyword refers to the current object.
It allows objects to access:
- Attributes
- Methods
Example
class Car:
def __init__(self, brand):
self.brand = brand
Object Methods
Methods are functions defined inside a class.
Example
class Student:
def __init__(self, name):
self.name = name
def greet(self):
print("Hello", self.name)
student1 = Student("Augustine")
student1.greet()
Output
Hello Augustine
Modifying Object Properties
Object properties can be changed after object creation.
Example
class Student:
def __init__(self, name):
self.name = name
student1 = Student("Augustine")
student1.name = "Brian"
print(student1.name)
Output
Brian
Deleting Object Properties
Python allows deleting object properties using "del".
Example
class Student:
def __init__(self, name):
self.name = name
student1 = Student("Augustine")
del student1.name
Inheritance in Python
Inheritance allows one class to inherit properties and methods from another class.
Example
class Person:
def __init__(self, name):
self.name = name
class Student(Person):
pass
student1 = Student("Augustine")
print(student1.name)
Output
Augustine
Polymorphism in Python
Polymorphism allows methods with the same name to behave differently.
Example
class Dog:
def sound(self):
print("Bark")
class Cat:
def sound(self):
print("Meow")
dog = Dog()
cat = Cat()
dog.sound()
cat.sound()
Output
Bark
Meow
Encapsulation in Python
Encapsulation restricts direct access to certain data.
Example
class BankAccount:
def __init__(self, balance):
self.__balance = balance
account = BankAccount(5000)
The double underscore "__" makes the attribute private.
Advantages of OOP
Object-Oriented Programming helps applications:
- Become modular
- Improve code reuse
- Enhance security
- Simplify debugging
- Handle large projects efficiently
Most enterprise applications rely heavily on OOP principles.
Real-World Applications of OOP
OOP is used in:
- Banking Systems
- School Management Systems
- Mobile Applications
- Web Applications
- Game Development
- Artificial Intelligence Systems
Conclusion
Object-Oriented Programming is one of the most important concepts in Python programming because it helps developers build organized, reusable, and scalable applications.
Understanding Classes, Objects, Inheritance, and Polymorphism is essential for becoming a professional Python developer.
Practice creating your own classes and objects to strengthen your Object-Oriented Programming skills.
Top comments (0)