Module 8: Introduction to Object-Oriented Programming (OOP)
Prepare for a new way of thinking. So far, we've written code as a sequence of steps. Now, we'll learn to structure our code by modeling the real world, a core principle in the **Ego Tech World** philosophy.
1. A New Paradigm: What is Object-Oriented Programming?
Up to this point, we've practiced **procedural programming**. We have data (in variables) and we have behavior (in functions), but they are separate. We write functions that take data as input, process it, and produce an output.
Object-Oriented Programming (OOP) is a programming paradigm that bundles related data and the functions that operate on that data together into a single unit called an **object**. Instead of a loose collection of variables and functions, you create self-contained objects that represent things, concepts, or entities.
🧠Core Concept: The Car Analogy
Think about a car. A car has data associated with it (its attributes): color, model, current speed, fuel level. It also has behaviors (its methods): `start_engine()`, `accelerate()`, `brake()`, `refuel()`.
In OOP, we create a "Car" object that holds both its attributes (`color = 'red'`) and its methods (`accelerate()`) together. The `accelerate()` method inherently knows how to modify the `current_speed` attribute of its own car. This bundling of data and behavior is the central idea of OOP.
2. The Blueprint and the Product: Classes and Objects
To create these objects, we first need a blueprint. In OOP, these concepts have specific names:
- A **Class** is the blueprint. It's a template that defines the attributes and methods that all objects of that type will have. The class itself is not the car; it's the design for the car.
- An **Object** is a specific **instance** of a class. It's the tangible product built from the blueprint. If `Car` is the class, then a specific red Ford Mustang and a specific blue Honda Civic are two separate objects (or instances) of that class.
3. Defining a Class in Python
Let's create a blueprint for a `Dog`. We use the `class` keyword to define it. By convention, class names start with a capital letter (PascalCase).
The `__init__` Method (The Constructor)
How do we give our dog a name and age when we create it? We use a special method called `__init__` (short for "initialize"). This method is the **constructor**; it runs automatically whenever a new object is created from the class.
The `self` Keyword
The `__init__` method's first parameter is always `self`. The `self` keyword refers to the specific instance of the object that is being created. It's how the object refers to itself. When we set `self.name = name`, we're saying, "Take the `name` that was passed in and store it as an **attribute** on this specific dog object."
Here, `name` and `age` are the **attributes** (the data) of our `Dog` class. They define the state of each dog object.
4. Instantiation: Bringing Objects to Life
Creating an object from a class is called **instantiation**. You do this by "calling" the class as if it were a function, passing in the arguments that the `__init__` method expects (after `self`).
Even though `dog1` and `dog2` were created from the same `Dog` class, they are completely independent objects in memory, each with its own `name` and `age` attributes.
5. Defining Behavior with Methods
A **method** is a function that belongs to a class. It defines the behavior of the objects. Let's add a `bark` method to our `Dog` class.
Just like `__init__`, every method in a class must have `self` as its first parameter. This allows the method to access and modify the object's attributes.
Notice how the `bark` method can use `self.name` to personalize its message. This is the power of bundling data and behavior together.
You've Completed Module 8!
This was a big conceptual leap! You've learned the fundamentals of Object-Oriented Programming, a paradigm used to build the vast majority of modern software. You can now create blueprints (classes) for objects and bring them to life with their own data (attributes) and behaviors (methods).
In the next module, we will build on this foundation by exploring the powerful advanced concepts that make OOP truly special: Encapsulation, Inheritance, and Polymorphism.
On to Module 9: Advanced OOP Concepts →