No history yet

Introduction to Classes

Blueprints for Code

In programming, we often need to model things from the real world. Think about a dog, a car, or a user account. Each of these has characteristics (like a name or color) and things it can do (like bark or drive).

Object-Oriented Programming (OOP) gives us a way to organize this complexity using a concept called a class. A class is like a blueprint. A blueprint for a house defines its structure—it has rooms, doors, and windows—but it isn't an actual house. It's just the plan.

Similarly, a class is a plan for creating objects. It bundles together data and the functions that operate on that data into a neat package.

A class is a template for creating objects. It defines the properties and behaviors that all objects of that type will have.

Attributes and Methods

A class has two main components: attributes and methods.

Attributes are variables that belong to a class. They store the data or characteristics of the object. For a Dog class, attributes might include its name, breed, and age.

Methods are functions that belong to a class. They define the object's behaviors or the actions it can perform. For our Dog class, methods could be bark() or fetch().

Let's look at a simple Dog class written in Python. It has attributes for name and age, and a method for barking.

class Dog:
    # The __init__ method is a special method that runs
    # when we create a new object from the class.
    # It's used to set up the object's initial attributes.
    def __init__(self, name, age):
        self.name = name  # Attribute
        self.age = age    # Attribute

    # A simple method
    def bark(self):
        return f"{self.name} says Woof!"

In this blueprint, name and age are attributes that will be unique to each dog we create. The bark() method is a behavior that all dogs created from this class will be able to perform.

Creating Objects from a Class

Once we have our class (the blueprint), we can create actual instances of it. This process is called instantiation, and the instances we create are called objects.

If the Dog class is our blueprint, we can create multiple Dog objects, each with its own specific name and age. Each object is a distinct entity, even though they were all created from the same blueprint.

# Instantiate (create) two Dog objects
my_dog = Dog("Buddy", 4)
your_dog = Dog("Lucy", 2)

Now we have two separate objects: my_dog and your_dog. We can access their individual attributes using dot notation.

print(my_dog.name)
# Output: Buddy

print(your_dog.name)
# Output: Lucy

We can also call their methods.

print(my_dog.bark())
# Output: Buddy says Woof!

print(your_dog.bark())
# Output: Lucy says Woof!

Even though both objects use the same bark() method defined in the Dog class, the output is customized because each object has its own name attribute. Classes allow us to create reusable, organized, and intuitive code by modeling real-world concepts.

Quiz Questions 1/6

In Object-Oriented Programming, a class is best described as a...

Quiz Questions 2/6

What are the variables that belong to a class and store the data or characteristics of an object called?

Classes are the foundational building block of object-oriented programming, providing a clean way to structure your code around the concepts it represents.