What is OOP?
Understanding Object-Oriented Programming and why it matters.
Introduction
So far in Week 1, you wrote Python programs using variables, loops, conditions, and functions. That style of programming is called procedural programming -- you write a sequence of instructions and the computer follows them from top to bottom.
But as your programs grow larger, procedural code can become messy and hard to manage. That is where Object-Oriented Programming (OOP) comes in. OOP is a different way of thinking about and organizing your code -- one that mirrors how the real world works.
What is a Paradigm?
A programming paradigm is simply an approach or style of writing code. Think of it as a philosophy for how to structure your programs.
Procedural Programming
Step-by-step instructions. You write functions that operate on data. The focus is on actions.
Object-Oriented Programming
Code organized around objects that bundle data and behavior together. The focus is on things.
The Four Pillars of OOP
OOP rests on four foundational concepts. We will explore each in depth over the coming weeks, but here is a quick overview:
Encapsulation
Bundling data and methods together, and controlling access to them.
Inheritance
Creating new classes based on existing ones, reusing their code.
Polymorphism
Using a single interface for different underlying forms or data types.
Abstraction
Hiding complex details and showing only the essential features.
Procedural vs OOP Comparison
Let us manage students both ways so you can see the difference.
Procedural approach:
student1 = {"name": "Alice", "age": 20, "grade": "A"}
student2 = {"name": "Bob", "age": 22, "grade": "B"}
def display_student(student):
print(f"{student['name']}, Age: {student['age']}, Grade: {student['grade']}")
display_student(student1)
display_student(student2)
OOP approach (preview):
class Student:
def __init__(self, name, age, grade):
self.name = name
self.age = age
self.grade = grade
def display(self):
print(f"{self.name}, Age: {self.age}, Grade: {self.grade}")
student1 = Student("Alice", 20, "A")
student2 = Student("Bob", 22, "B")
student1.display()
student2.display()
Notice: In the OOP version, each student carries its own data and its own behavior. The data and the function that operates on it live together inside the class. This is the core idea of OOP.
Why OOP Matters
- Code Reuse: Write a class once, create as many objects as you need.
- Organization: Related data and functions live together, making code easier to navigate.
- Real-World Modeling: OOP lets you represent real things naturally in code.
- Team Collaboration: Different team members can work on different classes independently.
Real-World Examples
Cars
The class is the car blueprint. Each car on the road is an object with its own color, brand, and speed.
Smartphones
The Phone class defines what every phone can do. Your specific phone is an object of that class.
Bank Accounts
The BankAccount class defines attributes (owner, balance) and methods (deposit, withdraw). Each customer's account is an object.
OOP Terms Glossary
- Class: A blueprint or template for creating objects. Like an architectural plan for a house.
- Object: A specific instance created from a class. Like an actual house built from the plan.
- Method: A function that belongs to a class. It defines what an object can do.
- Attribute: A variable that belongs to an object. It defines what an object has.
Best Practices
- Think about what things your program deals with -- those are candidates for classes.
- Start simple: not everything needs to be a class right away.
- Understand the concepts first; the syntax will come naturally with practice.
Common Mistakes
- Thinking OOP replaces procedural programming entirely -- both have their place.
- Trying to memorize the four pillars without understanding them. Focus on the idea, not just the name.
- Overcomplicating simple programs with unnecessary classes.
Practice Exercises
- In your own words, explain the difference between procedural and object-oriented programming.
- Name three real-world objects and list two attributes and two behaviors for each.
- Take the procedural student example above and add a third student. Then think about how the OOP version would handle it.
Summary
- OOP is a programming paradigm that organizes code around objects.
- The four pillars are Encapsulation, Inheritance, Polymorphism, and Abstraction.
- Classes are blueprints; objects are instances created from those blueprints.
- OOP helps with code reuse, organization, and real-world modeling.