Object-Oriented Programming (OOP) is a programming paradigm that organizes code using objects — instances of classes that combine data and functionality. Python supports OOP fully, making it a great language for building reusable, modular, and scalable applications.
This guide introduces you to the core concepts of OOP in Python, with simple examples to help you get started.
1. What is Object-Oriented Programming?
In OOP, you model real-world entities using classes (blueprints) and objects (instances). Each object can hold attributes (data) and methods (functions that operate on data).
2. Benefits of OOP
- Code reusability through classes and inheritance
- Better organization and structure
- Encapsulation of data
- Easier to maintain and extend large applications
3. Classes and Objects
Creating a Class
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def greet(self):
return f"Hello, my name is {self.name}."
Creating an Object (Instance)
person1 = Person("Alice", 30)
print(person1.greet())
Output:
Hello, my name is Alice.
4. The __init__
Method
The __init__
method is the constructor. It runs automatically when you create a new object and is used to initialize attributes.
class Dog:
def __init__(self, breed):
self.breed = breed
dog1 = Dog("Labrador")
print(dog1.breed)
5. Instance vs Class Attributes
Instance attributes are unique to each object, while class attributes are shared across all instances.
class Car:
wheels = 4 # Class attribute
def __init__(self, color):
self.color = color # Instance attribute
car1 = Car("Red")
car2 = Car("Blue")
print(car1.wheels, car2.color)
6. Methods
Methods are functions defined inside a class. The first argument is always self
, which refers to the instance calling the method.
class Circle:
def __init__(self, radius):
self.radius = radius
def area(self):
return 3.14 * self.radius * self.radius
7. Encapsulation
Encapsulation restricts direct access to some parts of an object. In Python, you use underscores to indicate private attributes (by convention).
class Account:
def __init__(self, balance):
self.__balance = balance # Private attribute
def get_balance(self):
return self.__balance
8. Inheritance
Inheritance allows a class to inherit attributes and methods from another class, enabling code reuse.
class Animal:
def speak(self):
return "Makes a sound"
class Dog(Animal):
def speak(self):
return "Barks"
dog = Dog()
print(dog.speak())
9. Polymorphism
Polymorphism allows different classes to implement the same method in different ways.
class Bird:
def sound(self):
return "Chirp"
class Cat:
def sound(self):
return "Meow"
def make_sound(animal):
print(animal.sound())
make_sound(Bird())
make_sound(Cat())
10. Summary of OOP Concepts
Concept | Description |
---|---|
Class | A blueprint for creating objects |
Object | An instance of a class |
Attribute | Variable stored inside an object |
Method | Function defined inside a class |
Encapsulation | Restricting direct access to data |
Inheritance | Using properties of a parent class in a child class |
Polymorphism | Same method name behaving differently across classes |
11. Final Thoughts
Understanding Object-Oriented Programming is essential for building organized, efficient Python applications. Start small with your own classes and gradually apply OOP concepts like inheritance and encapsulation as your projects grow. Mastering OOP will make your code more reusable, testable, and easier to maintain over time.