Latest :

Parameterized Constructor In Java – Tutorial & Examples

What is Parameterized Constructor in Java – Tutorial & Examples – If we want to set some data to the constructor so that it can be used in the constructor then we can send so like

Person r=new Person(10,20);

to receive the values, the corresponding constructor should have formal arguments like.

If we want to send three values to constructor like Person r=new Person(15,25,35); then the constructor should be like

We can have any number of constructors in a class but we should see their arguments are different.

Example:

Note: The system does not generate the default constructor if we create any other constructor. In the above example, we have prepared 2 constructors that take arguments. So the default constructor is not available in the class.

Hence the statement Person r1=new Person(); becomes invalid. So, it is generally suggested to create the default constructor along with argumented constructor.

If we want that the object should be created with arguments only, then we can omit the default constructor.

  • When an object is created, its constructor is called and the code in that constructor is executed.
  • By default, the default constructor is called when we create an object.
  • If we specify any arguments along with object creation, then the corresponding argumented constructor will be executed.

Example:

Objects as arguments to the constructor

We can pass an object as an argument to the constructor. The constructor of one class can take an object of any class as argument. There is no restriction on the number of objects passed to the constructor.

Example:

Chained constructors:

Calling one constructor from another constructor is referred to as constructor chaining. This can be achieved using this() or super()

Suppose we want to execute code of more than one constructor with an object creation, then we call one constructor from another constructor so that both will be executed.

This mechanism of calling more than one constructor i.e. one constructor calling another constructor; with a single object creation is known as constructor chaining.

The this() is used to call one constructor from another constructor of the same class.

Example:

If we want to call the default constructor then this() is used without any arguments. If we want to call an argumented constructor then this() is used with arguments. We can call any number of constructors in this way.

But an already called constructor should not be called again in the sequence. It means constructors should not be called recursively.  So the following code is not valid.

Example:

The this() should be the first statement if it exists. So the following code is invalid.

Example:

We can call the constructor of the current class with this() method. When a child class object is created, the object of the parent is also created as part of the child object.

As parent object is created its constructor should also be called. In this case, the default constructor of the parent class is called with child class constructor.

If we want an argumented constructor of the parent to be called with a child class constructor then we can exclusively call that using super(). Using super() without arguments will call the default constructor of the parent class.

  • To call any argumented constructor of the parent class we should use super() with arguments.
  • When a child object is created, even though the control comes to child class constructor first, it will not execute the code in child constructor.
  • From child class constructor it goes to the parent class constructor and executes it. After that, it comes back to execute the child class constructor. So the parent constructor is executed before the child constructor.

Suppose 3 classes are involved (say A, B and C where C is the child of B and B is the child of A) and third class (C) object is created, then control goes to C’s constructor.

But before executing the statements in that it goes to B’s constructor. even in B, before executing B’s statements, it goes to A’s constructor. after executing the A’s constructor, the control comes back to execute B’s constructor.

Once that execution is over, the control comes to C. Like this(), the super() also be the first statement in a constructor, if it exists. So the following code is invalid.

Example:

As this() & super() should be the first statement (if exists) in a constructor, we cannot use them in a single constructor at a time. So the following code is not valid.

Example:

The this() & super() both involved

Though it is not allowed to have this() and super() together they can be used in constructor chaining so that they get executed.

Note: if we have a constructor without a super() or this(), then the system automatically assumes super() as existing. So the following two codes are the same.

Example 1:

Example 2:

What is a Constructor

  • A constructor is a special method that is called when a new object is created.
  • If we want to perform any activity on an object at the time of its creation, then the constructor is a good place. Generally, instance variables are initialized in the constructor.
  • Along with initializations, we can do any activity in the constructor that we do in a method. The difference is constructor is executed automatically and the normal method is explicitly called.
  • Constructor name and its class name should be same.
  • The Constructor does not have a return type.
  • For each object, the constructor is called that too when the object is created.
  • Constructors can also take arguments, so can be overloaded.
  • The constructor without any arguments is a default constructor, the other constructors are parameterized constructors.
  • We need not to exclusively call the constructor, it is automatically called when the object is created.

Example:

When an object is created the instance variables of the object are automatically set with their default values. If we specify any values, then the default values will be overridden.

Default constructors

The constructor without any arguments is known as default constructor. This constructor is generated by the system if we do not create any constructors.

The system generated constructor does not contain any code. For a class named Person, the system created default constructor would be like

We can define the default constructor on our own. It is like overwriting the default constructor. We can write any code in it. When we create the default constructor, the system does not create the default constructor.

Example:

So, a class can have only one default constructor.

x

Check Also

Convert String To Date In Java – JavaTutoring

Convert String to Date In Java – Here we cover the different ways to convert ...