⇧
vadnica-logo
My Services About the Project Telegram O meni O meni

OOP Basics: Constructor

A constructor is a special, so-called "magic" method within a class that triggers automatically at the moment you create a new instance of the class using the new command. Its main task is to give the object initial values right at birth (initialization), which ensures that the object is ready for use without any additional settings.

Imagine the constructor as a factory robot on an assembly line. When you order a new car (create an object), the robot makes sure the car does not leave the factory empty – it immediately installs the chosen brand, engine, and color. Without a constructor, you would get a "raw" chassis, and then you would have to manually set each property individually in the code, which would take a huge amount of time and open the door to numerous errors.

EXAMPLE
RESULT
The result will be displayed here...

What is Worth Remembering About Constructors?

When designing your classes, always consider the following four golden rules of constructor operation: