A constructor is a subroutine that returns a new object. An object in this context is a bundle of data that represents an abstraction of something. Some constructors just allocate memory and populate it with default values. Others might register the new object in a master list or other controller object, or could even initialize hardware in the real world.
The opposite is a destructor, which accepts an object and de-allocates it and does anything else needed to maintain class distance of the overall program’s memory. That might include deleting references to the object from elsewhere, or even sending command sequences out to actual hardware.
To ELI5 this, in object oriented programming you define various classes to represent *objects* you intend to instantiate (allocate memory for) and use for some purpose. It might be a person, a database connection, some data representation of something, some object that performs some task, etc.
The constructor *constructs* the object, meaning, memory is allocated and it becomes ready for use.
If you don’t define one, the compiler will create a default constructor for you.
You can parameterize a constructor such as creating a database connection by passing in a connection string to the server on the network where it exists, so it will be able to connect to it when the object is created.
It allocates memory, and performs tasks to make the object ready for use.
There’s lots more detail on this beyond this, but that’s a basic explanation.
A builder has a schematic or plans to build a house, they then get all the parts to *construct* that house, and parameters may be what type of wood, brick, appliances, wiring, etc. to make the house to the customer specifications.
A constructor is a function that is automatically called when an object is created. Each class has its own constructor.
Say there’s a Car class. When I make a new car, I probably want to specify what kind of car I want, like what color, what make, etc. Then I can make a new car with `new Car(“blue”, “Honda”, …);`.
That sure looks like a function call, right? Well, it is. You make a new object belonging to the Car class, and the Car class automatically pipes those parameters to the constructor function you specified in the definition of Car – usually, to set some properties of the object depending on what parameters you passed in.
A constructor is used to construct a new object of this class. I’m gonna assume that you know about what a class and an object as an instance of a class is based on how you worded the question, if not feel free to ask for a follow-up.
Let’s say you have a class rectangle. Every rectangle is defined by the two opposite corner points, so it doesn’t make any sense to have a rectangle without those two points. Your constructor for an rectangle object therefore takes two parameters and might check them for validity, e.g. not the same point twice etc.
This way, you can always ensure that your rectangle is valid and can e.g. be drawn to the screen.
The main idea behind object oriented programming is that data is inside objects that can only be changed through some operation that guarantee the object is valid all the way.
Imagine I give you two devices. One only has two buttons, Play music and Stop music.
The other has all kinds of visible wires, you can touch the wires with each other to do all kinds of things, including things that can break the device.
That is why, in object oriented programming, you can only interact with data in an instance through some operations that are called “public”. A well designed class should allow you to change things freely, but only through some operations that guarantee you can’t break it.
Now, you say, how do you guarantee that the first, initial state, of the object is a valid one? Well, through something called a constructor. You should think of a constructor as a function that creates a valid object of that class.
In JavaScript, well, things are weirder, but the idea is the same, a constructor is what allows you to build an instance of a class.
Crash course Object Oriented Programming: A class describes a type, like “Dog” or “Person”. An object is an instance of that type, like “Fido” is an object of type “Dog”. A method is a function defined as part of the class, and called on an object. For example, “feed()” might be method of “Dog” and you can then call “Fido.feed()” to feed Fido the Dog.
Ok, with that knowledge, we can now explain that a constructor is a special method which is called whenever a new object is created.
The intended purpose of the constructor is that it sets up all the internal data of the object, but you can do whatever you want with it.
A class definition defines what a class looks like. What attributes objects of that class have, what methods they have, etc. Think of it like saying “a chair must have legs/some kind of support, a seat, and a back.”
A constructor creates an instance of a class, giving values to those attributes. Think of it like building an actual chair out of specific legs, seat and back.
Remember that a class is a definition of what it means to be a chair, and an instance/object of that class is an actual chair. Constructors build specific chairs.
Your question has been answered perfectly before me, so I’ll offer an alternate method to answer your question. You can use GPT to answer your question, but not only answer it in technical terms but also (if you prefer it) tailor it so you can understand it better.
My query would be: “Can you explain it to a layman/5 yo?” or “Can you provide a real life analogy?” or “Can you provide an example but make it so the code is english-like?” And so on until you really understand the concept. The beauty of GPT is you can tailor your question relative to your understanding and it will also tailor the answer accordingly.
If you have ever placed an online order (or kiosk order in person) for a fast food place, that’s a great way to think about a constructor. You tell it what kind of food item you want, and then specify the various options for it.
Give me a burger with 2 patties, 2 slices of American cheese, and pickles and mustard for toppings.
Burger would be the object here, and it would then have fields for number of patties, number of slices of cheese, a list of toppings. You the call the constructor like “new Burger(2, 2, {pickles, mustard})” and this submits the instruction to make a burger object with the specified parameters. This object can then be passed around and used as a unique instance of the class, and you can instantiate multiple objects of the same type without overwriting each other.
Latest Answers