What is Object-Oriented Programming?
Object-Oriented Programming (OOP) is a programming paradigm based on the concept of "objects", which can contain data in the form of attributes (properties) and code in the form of methods (functions). OOP helps organize complex programs by creating reusable, modular components that mirror real-world entities.
Classes and Objects: The Foundation
- A class is a blueprint or template for creating objects.
- An object is an instance of a class— a concrete realization of that blueprint.
class Person:
def __init__(self, fname, lname):
self.firstname = fname
self.lastname = lname
def printname(self):
print(self.firstname, self.lastname)
# Create an object (instance) of the Person class
person1 = Person("John", "Doe")
person1.printname() # Output: John DoeThe __init__() Method: The Constructor
The __init__() method is a special method in Python classes. It is automatically called when a new object is created from the class. It initializes the object's attributes.
- Always include
selfas the first parameter—it refers to the current instance. - You can pass additional parameters to customize each object.
Inheritance: Reusing and Extending Code
Inheritance allows a new class (called the child or derived class) to inherit attributes and methods from an existing class (called the parent or base class).
This promotes code reuse and establishes a natural hierarchy among classes.
# Parent class
class Person:
def __init__(self, fname, lname):
self.firstname = fname
self.lastname = lname
def printname(self):
print(self.firstname, self.lastname)
# Child class
class Student(Person):
pass # Inherits everything from Person
# Use the Student class
student1 = Student("Mike", "Olsen")
student1.printname() # Output: Mike OlsenAdding the __init__() Function to Child Class
When you add an __init__() method to the child class, it overrides the parent's __init__() method. To preserve the parent's initialization, you must explicitly call it.
__init__()class Student(Person):
def __init__(self, fname, lname):
Person.__init__(self, fname, lname) # Manual callUsing the super() Function
The super() function is a more elegant and Pythonic way to inherit from the parent class. It automatically refers to the parent class, so you don’t need to name it explicitly.
super() for Cleaner Codeclass Student(Person):
def __init__(self, fname, lname):
super().__init__(fname, lname) # No need to mention 'Person'Adding Properties to the Child Class
You can extend the child class by adding new attributes that the parent class doesn’t have.
class Student(Person):
def __init__(self, fname, lname, year):
super().__init__(fname, lname)
self.graduationyear = year # New property
student1 = Student("Mike", "Olsen", 2019)
print(student1.graduationyear) # Output: 2019Adding Methods to the Child Class
Child classes can also define new methods or override existing ones from the parent class.
class Student(Person):
def __init__(self, fname, lname, year):
super().__init__(fname, lname)
self.graduationyear = year
def welcome(self):
print("Welcome", self.firstname, self.lastname,
"to the class of", self.graduationyear)
student1 = Student("Mike", "Olsen", 2019)
student1.welcome()
# Output: Welcome Mike Olsen to the class of 2019Method Overriding
If a child class defines a method with the same name as a method in the parent class, the child’s version overrides the parent’s. This is a core feature of polymorphism.
class Person:
def introduce(self):
return "I am a person."
class Student(Person):
def introduce(self):
return "I am a student."
s = Student()
print(s.introduce()) # Output: I am a student.Key Benefits of OOP
- ✅ Modularity: Code is organized into self-contained classes.
- ✅ Reusability: Inheritance lets you reuse code without duplication.
- ✅ Maintainability: Changes to one class rarely affect others.
- ✅ Scalability: Easy to add new features by extending existing classes.
Best Practices
- ✅ Always use
super()instead of hardcoding the parent class name. - ✅ Keep classes focused—follow the Single Responsibility Principle.
- ✅ Use meaningful class and method names that reflect real-world concepts.
- ✅ Document your classes with docstrings to explain their purpose.
Common Mistakes to Avoid
- ❌ Forgetting
selfin method definitions. - ❌ Not calling
super().__init__()when adding__init__to a child class. - ❌ Creating deep inheritance chains—prefer composition when possible.
- ❌ Overriding methods without understanding the parent’s contract.
Summary: The OOP Workflow
- Define a parent class with common attributes and methods.
- Create a child class that inherits from the parent.
- Use
super()to initialize inherited attributes. - Add new properties and methods to extend functionality.
- Override methods when you need different behavior in the child class.