Welcome to Python OOP

Classes, objects, constructors, and the core principles: inheritance, polymorphism, and encapsulation.

OOP overview

Introduction to OOP: Classes, Objects, Attributes, and Methods (1 hour)

📘 Introduction to OOP: Classes, Objects, Attributes, and Methods (1 hour)

What’s OOP?

Imagine everything as objects — a car 🚗, a book 📚, or even a cute little puppy 🐶. OOP helps us model complex systems by treating real-world entities as objects within the program. We assign them classes (their blueprint), attributes (their characteristics), and methods (their actions)!

Key Concepts:

Class: The blueprint or prototype, like a recipe! Defines the structure for creating objects.

Object: A specific instance of a class. Think of it as a cake made from that recipe 🎂.

Attributes: Characteristics or properties of an object, like the color of a car.

Methods: Actions or behaviors that the object can perform (e.g., drive() for a car).

Example: Create Your First Class!

# Defining a class
class Car:
  color = "red" # Attribute
  # Method
  def drive(self):
    print("The car is driving")

# Creating an object
my_car = Car()
print(my_car.color)
my_car.drive()

🔧 Constructors: The __init__ Method and Instance Variables

What’s a Constructor?

Every time you create a new object, you want it to have its unique attributes (color, model, etc.). Constructors (__init__ methods) allow each object to start with specific values. It’s like building a pizza 🍕 with the toppings you want!

Instance Variables

Instance variables are specific to each object and can vary across objects. For example, two Car objects can have different colors, models, and speeds.

Example: Setting Up Your Constructor!

class Car:
  def __init__(self, color, model):
    self.color = color # Instance variable
    self.model = model # Instance variable

# Creating objects with unique attributes
car1 = Car("blue", "Sedan")
car2 = Car("red", "SUV")
print(car1.color) # Output: blue
print(car2.model) # Output: SUV

🧩 OOP Principles: Inheritance, Polymorphism, and Encapsulation (1.5 hours)

Inheritance: Just like humans inherit traits from their parents, classes can inherit attributes and methods from other classes. This reduces code repetition and creates a natural hierarchy!

Example:

class Vehicle:
  def __init__(self, wheels):
    self.wheels = wheels

class Car(Vehicle):
  pass

car = Car(4)
print(car.wheels) # Output: 4

Polymorphism: Derived classes can behave differently for the same method inherited from a base class. A method name can mean different things across multiple classes.

Example:

class Dog:
  def speak(self):
    return "Woof!"

class Cat:
  def speak(self):
    return "Meow!"

for animal in [Dog(), Cat()]:
  print(animal.speak())

Encapsulation: Keeping attributes and methods private to prevent unwanted interference from outside the class. It’s like hiding your chocolate stash 🍫!

Example:

class SecretStash:
  def __init__(self):
    self.__chocolates = 10 # Private attribute

  def take_chocolate(self):
    if self.__chocolates > 0:
      self.__chocolates -= 1
      print("One chocolate taken!")
    else:
      print("No chocolates left")

stash = SecretStash()
stash.take_chocolate()

In Summary: OOP allows you to organize code in a fun, reusable, and efficient way! Imagine real-world objects and think of how they could become classes. Whether it’s a Smartphone, Pet, or Superhero, OOP helps you build programs that feel like real-world systems.

Happy coding! 🎉