DAY 24

OOP 🤖🧱🏗️

Model real-world things in code. A class is a blueprint, an object is the real thing built from it!

⏱ 15 mins
⚡ +50 XP
OOP 🤖🧱🏗️

Day 24: Object-Oriented Programming — Code Like the Real World!

What's OOP?

Think about a house. Before building it, an architect makes a blueprint. The blueprint isn't a real house — it's just the plan. But from that one blueprint you can build 100 identical houses. In Python, a class is the blueprint and an object is the real house built from it. This is Object-Oriented Programming — modelling real things in code!

Your First Class


class Dog:
    pass

dog1 = Dog()
dog2 = Dog()

print(type(dog1))  

class Dog creates the blueprint. Dog() builds a real object from it. dog1 and dog2 are two separate dogs built from the same blueprint. One class, many objects!

Adding Properties to a Class


class Dog:
    def __init__(self, name, breed):
        self.name = name
        self.breed = breed

dog1 = Dog("Bruno", "Labrador")
dog2 = Dog("Max", "Poodle")

print(dog1.name)    # Bruno
print(dog2.breed)   # Poodle  

__init__ runs automatically when you create an object — it sets up the properties. self means "this specific object." dog1 and dog2 are both Dogs but have different names and breeds. Same blueprint, different details!

Real World Connection

Every Instagram user is an object built from a User class — same blueprint, different username, followers and posts. Every product on Amazon is an object from a Product class — same blueprint, different name, price and stock. Every message on WhatsApp is an object from a Message class. OOP is how every real app organises its data!

Adding Actions to a Class


class Dog:
    def __init__(self, name):
        self.name = name

```
def bark(self):
    print(self.name, "says: Woof!")
```

dog1 = Dog("Bruno")
dog1.bark()  

Output: Bruno says: Woof! Functions inside a class are called methods. They're actions the object can do. A User object can login(). A Product object can add_to_cart(). A Message object can send()!

Common Mistakes

Mistake 1 — Missing colon after class name.


class Dog      # WRONG — missing colon!
class Dog:     # CORRECT

Mistake 2 — Forgetting to create an object.


class Dog:
    pass
Dog.bark()       # WRONG — Dog is just a blueprint!

dog1 = Dog()
dog1.bark()      # CORRECT — dog1 is a real object!  

Mini Challenge

Mini Challenge

Create a Student class with name and grade properties. Add a method called introduce() that prints "Hi I'm [name] and I got grade [grade]!". Create two student objects with different names and grades and call introduce() on both. You just built the same user system every school app uses!

Quick Quiz

Q: What's the difference between a class and an object? A: Class is the blueprint. Object is the real thing built from it!

Q: What does __init__ do? A: Runs automatically when an object is created — sets up its properties!

Q: Can one class create many objects? A: Yes! One blueprint, unlimited real objects!

Key Takeaways

Key Takeaways

  • A class is a blueprint. An object is the real thing built from it.
  • __init__ sets up an object's properties when it's created.
  • self refers to the specific object being created or used.
  • One class can create unlimited objects — same blueprint, different details.
  • Every app models real things as classes and objects — users, products, messages!

← Previous Lesson