Tuesday, March 19, 2024 05:30

Table of contents >> Objects > Constructors

Constructors

In object-oriented programming, when creating objects from given classes, it is sometimes necessary to call some special methods of those classes, known as a constructors.

Constructor of a class is a pseudo-method, which does not have a return type, has the name of the class and is called using the keyword new. The task of the constructor is to initialize the memory allocated for the object, where its fields will be stored (those which are not static ones – we will learn about what static means in the future).

Let’s take again our Oven class, from two lessons ago:

In the previous lesson, we have learned what fields and properties are, and we already know the concept of methods. So, in the above code, there is only one piece left that we did not yet analyze. Namely, this one:

The above semi-method is called the constructor of our class. Why do i call it semi-method? Well, you can clearly see that it has no return type, which rules it out of being a function, but it also doesn’t use the void keyword, like any method should.

Another thing you may have noticed is that the identifier (name) of the constructor is the same as the identifier of the class. Lets see what would have happened if we would have named our constructor differently:

The above code would give us an error: Method must have a return type.  So, the compiler doesn’t recognize it as a constructor anymore, and rather tries to treat it as a regular function. From this, the following conclusion derives: in C# it is mandatory that the name of every constructor matches the name of the class in which it resides. And also, it is not allowed to declare a method whose name matches the name of the class (hence the name of the constructors). If nevertheless, a method is declared with the class name, this will cause a compilation error ‘Oven’: member names cannot be the same as their enclosing type.

The way we declare a constructor is somehow similar to methods:

About modifiers, we will learn in the future, though we encountered a few of them already (private, public, static).  Aside of that, the parameter list is similar to the way we declare methods with parameters. In the next lesson, we will learn how to create copies of the blueprint of our objects. But, as we learned in the properties lesson, our copies would not be of much use, if we couldn’t individualize them:

That works fine, but takes a lot of lines, specially if we have to specify a lot of properties. Instead, we could do it directly when we create the object copy, using a constructor with parameters. Let’s say we have this:

after that, we can create our object copy like this:

Much more short and elegant to define a new copy! One thing to notice here – we defined our parameter variables with the same name as our fields. That is totally acceptable. Also, notice that the scope of constructors parameters is identical to any method’s parameters scope: it is only available for the body of the constructor. Though, there is a trap regarding this. I explained before that the this keyword is optional, and indicates that we are referring to stuff related to the current copy of the object we are manipulating at that particular time. So, we could have written this:

but in this case, we would make a fatal mistake, because without the keyword this, we are actually not referring to our fields, but to the parameter variables of the constructor itself. So, instead of assigning the value of the parameters to our fields, we are actually assigning the values of the parameter variables to themselves. This is one of the reasons why I said that though optional, we should always use this when referring to an instance.

Other similarities with Methods is that our constructors can have optional parameters, and also we can have constructors overloading:

What happens when we don’t provide any constructor at all? As in many other cases, the compiler will create a default constructor for us, which is empty, has no parameters and no code in its body, and does nothing else aside of initializing our fields with their type default value. This kind of constructor is called default implicit constructor. This means that it is not mandatory to create a class with a constructor, if we don’t need any initialization code upon instantiating our class. Finally, you should know that a default implicit constructor does not have the same signature as a constructor without parameters! A default implicit constructor will always have the access level protected or public (we will learn about this in the future) – depending on the access level of the class itself, while a constructor without parameters specifically declared by us will have whatever access level we provide it.

In the next lesson, we will also see the way of calling constructors, since they cannot be called like any usual method call.

To conclude, the constructor is the entry point of our class, just as Main() method is the entry point of our program: it is the first thing to be executed when instantiating a class. If we want to run any code automatically when we create a copy of a class, we do it using constructors.

Tags: , ,

Leave a Reply



Follow the white rabbit