Sobes.tech
Junior — Middle

Can you explain what it means to create an object of a class in programming?

sobes.tech AI

Answer from AI

Creating an object of a class in programming is the process of allocating memory and initializing an instance of that class, which can then be used to call methods and access properties.

In Python, an object is created when the class constructor is called, usually by calling the class name with parameters if needed.

Example:

class Car:
    def __init__(self, brand, year):
        self.brand = brand
        self.year = year

# Creating an object of class Car
my_car = Car("Toyota", 2020)
print(my_car.brand)  # Outputs: Toyota

Here, my_car is an object (instance) of the class Car, which stores data and behavior defined in the class.