YELKHANBLOG
Simply Explained: Classes
The center of Object Oriented Programming
If you found this article, you probably already know precisely what you need to learn about. Let us begin.

Classes are bases upon which instances are built. If you don't understand what that means, let me explain.

If I tell you the word "car", you will probably think of the abstract concept of a car. It has four wheels, is made of metal, and can be driven. We could say that this is a class.

An instance would be a single individual car. Your car is an instance. It has a unique model name, color, number of seats, and a serial number (VIN… whatever) that together make it a unique individual object; however, it also has four wheels, is made of metal, and can be driven. Therefore, we can say that your car is an instance of the "car" class.

If estimates say that there are 1.42 billion cars worldwide, then we can say that there are 1.42 billion instances of the "car" class.
Let's write this in code.

class Car:
  def __init__(self, model_name, color, seats, serial_number):
    self.model_name = model_name
    self.color = color
    self.seats = seats
    self.serial_number = serial_number

  def drive(self):
    return("Vroom, vroom!")

#these aren't actual vehicle serial numbers but whatever
car_instance_1 = Car("Toyota Camry", "gray", 5, "0123456")
car_instance_2 = Car("Audi A4", "red", 5, "6543210")
car_instance_3 = Car("Ford Mustang", "yellow", 2, "3456210")

print(car_instance_1.model_name)
print(car_instance_2.seats)
print(car_instance_2.drive())
In the first block, we create a "car" class using the special __init__ method. Every instance has a model name, color, number of seats, and a serial number. Variables inside of instances are called parameters. Then, we create a drive() function that prints a message. Functions inside of instances are called methods.

Therefore, our class has four parameters: model_name, color, seats, and serial_number. It also has one method: drive()

In the second block, we create three instances of the car class and assign unique parameters to them. Think of these parameters as variables inside of variables (instances, in this case).

Then, we print two parameters and use the drive() method, which will output the following:

Toyota Camry
5
Vroom, vroom!


Did you know that while programming, you were creating instances all this time? Let's take a string as an example.


hello_world = "Hello World!"
my_car is a car, or an instance of the car class. hello_world is a string, or an instance of… wait…

Yes, this means that the string is a class too! In this case, hello_world is an instance of the string class.

Do you now see the importance of classes in programming? Almost every variable that you create is an instance of a class!

You can easily see that by learning how to work with classes, you will master one of the core concepts of programming as a whole. This article has been your introduction to them.

Thanks for coming to my TED Talk.
Made on
Tilda