Constructor of a class

In every class, there is a special type of method, called a constructor. You can create a constructor in a class and program it. If you don't create one yourself, the compiler will create a very simple constructor and use that instead. Let's take a look at what the constructor is and what it does.

A constructor is a method that gets triggered when an object of a class is created. A constructor is mainly used to set the prerequisites of the class. For example, if you are creating an object of the Human class, that human object must have a date of birth. Without a date of birth, no human can exist. You can set this requirement in the constructor. You can also configure the constructor to set the date of birth as today if no date of birth is given. This depends on the needs of your application. Another example could be a bank account object, for which you have to provide the bank account holder. No bank account can exist without an owner, so you can set this requirement in the constructor.

Let's take a look at the general form of a constructor, as shown in the following code:

access-modifier classs-name(parameter-list) {
// constructor body
}

Here, we can see that there is a difference between a constructor and a normal method, namely that a constructor doesn't have a return type. This is because a constructor can't return anything; it's for initialization, not for any other type of action. Normally, the type of access is public for constructors, because otherwise no object can be instantiated. If you specifically want to prevent objects of a class from being instantiated, you can set the constructor as private. Let's look at an example of a constructor, as shown in the following code:

class BankAccount {
public string owner;

public BankAccount(){
owner = "Some person";
}
}

In this example, we can see that we have a class called BankAccount and that it has a variable called owner. As we know, no bank account can exist without an owner, so we need to assign a value to the owner when an object is created. In order to create a constructor, we just make the access type of the constructor public, as we want objects to get instantiated. We can also take the name of the owner of the bank account as a parameter in the constructor and use it to assign the variable, as shown in the following code:

class BankAccount {
public string owner;

public BankAccount(string theOwner){
owner = theOwner;
}
}

If you put parameters in the constructor, then, when initializing the object, the parameters need to be passed, as shown in the following code:

BankAccount account = new BankAccount("Some Person");

Another interesting thing is that you can have multiple constructors in a class. You might have one constructor that takes one argument and another that doesn't take any arguments. Depending on the way in which you are initializing the object, the respective constructor will be called. Let's look at the following example:

class BankAccount {
public string owner;

public BankAccount(){
owner = "Some person";
}

public BankAccount(string theOwner){
owner = theOwner;
}
}

In the preceding example, we can see that we have two constructors for the BankAccount class. If you pass a parameter when you create a BankAccount object, it will call the second constructor, which will set the value and create the object. If you don't pass a parameter while creating the object, the first constructor will be called. If you don't have either one of these constructors, this method of object creation won't be available.

If you don't create a class, then the compiler creates an empty constructor for that class, as follows:

class BankAccount {
public string owner;

public BankAccount()
{
}
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.59.192