Skip to content Skip to sidebar Skip to footer

Can We Call Constructor Again After Updating Field Variables

Prerequisite – Constructors in Java
Constructor chaining is the procedure of calling 1 constructor from some other constructor with respect to current object.
Constructor chaining tin exist done in two ways:

  • Within same class: It can be done using this() keyword for constructors in same class
  • From base class: past using super() keyword to call constructor from the base of operations class.

Constructor chaining occurs through inheritance. A sub class constructor's task is to call super class's constructor first. This ensures that creation of sub class's object starts with the initialization of the data members of the super class. There could be any numbers of classes in inheritance chain. Every constructor calls upwardly the chain till form at the pinnacle is reached.
Why do nosotros need constructor chaining ?
This process is used when nosotros want to perform multiple tasks in a single constructor rather than creating a code for each chore in a single constructor we create a separate constructor for each task and brand their chain which makes the program more readable.

Constructor Chaining inside same form using this() keyword :

Constructor Chaining In Java

Java

form Temp

{

Temp()

{

this ( 5 );

Organization.out.println( "The Default constructor" );

}

Temp( int x)

{

this ( 5 , fifteen );

System.out.println(x);

}

Temp( int ten, int y)

{

System.out.println(10 * y);

}

public static void main(String args[])

{

new Temp();

}

}

Output:

75 five The Default constructor

Rules of constructor chaining :

  1. The this() expression should e'er be the first line of the constructor.
  2. In that location should be at-least exist one constructor without the this() keyword (constructor iii in above example).
  3. Constructor chaining can exist achieved in any order.

What happens if we modify the guild of constructors?
Zilch, Constructor chaining can be achieved in any order

Java

class Temp

{

Temp()

{

Arrangement.out.println( "default" );

}

Temp( int x)

{

this ();

System.out.println(ten);

}

Temp( int x, int y)

{

this ( 5 );

System.out.println(10 * y);

}

public static void main(String args[])

{

new Temp( eight , 10 );

}

}

Output:

default 5 fourscore

NOTE: In instance i, default constructor is invoked at the end, but in example 2 default constructor is invoked at first. Hence, order in constructor chaining is not important.

Constructor Chaining to other class using super() keyword :

Java

class Base

{

String name;

Base of operations()

{

this ( "" );

System.out.println( "No-argument constructor of" +

" base of operations form" );

}

Base of operations(String name)

{

this .proper name = name;

System.out.println( "Calling parameterized constructor"

+ " of base" );

}

}

class Derived extends Base

{

Derived()

{

Organization.out.println( "No-statement constructor " +

"of derived" );

}

Derived(String name)

{

super (name);

System.out.println( "Calling parameterized " +

"constructor of derived" );

}

public static void primary(String args[])

{

Derived obj = new Derived( "test" );

}

}

Output:

Calling parameterized constructor of base Calling parameterized constructor of derived

Note : Similar to constructor chaining in same form, super() should exist the beginning line of the constructor equally super class's constructor are invoked before the sub course'southward constructor.
Culling method : using Init block :
When we want sure common resources to be executed with every constructor we tin put the code in the init cake. Init cake is always executed before whatsoever constructor, whenever a constructor is used for creating a new object.
Example ane:

Java

class Temp

{

{

System.out.println( "init block" );

}

Temp()

{

System.out.println( "default" );

}

Temp( int x)

{

System.out.println(x);

}

public static void main(String[] args)

{

new Temp();

new Temp( 10 );

}

}

Output:

init cake default init cake x

NOTE: If there are more 1 blocks, they are executed in the lodge in which they are defined within the same class. See the ex.
Instance :

Coffee

class Temp

{

{

System.out.println( "init" );

}

Temp()

{

Arrangement.out.println( "default" );

}

Temp( int 10)

{

Arrangement.out.println(x);

}

{

Organization.out.println( "2d" );

}

public static void primary(Cord args[])

{

new Temp();

new Temp( 10 );

}

}

Output :

init second default init second ten

This commodity is contributed by Apoorva singh. If you similar GeeksforGeeks and would similar to contribute, yous can also write an commodity using write.geeksforgeeks.org or mail your article to review-squad@geeksforgeeks.org. Run into your commodity appearing on the GeeksforGeeks main page and assistance other Geeks.
Please write comments if yous observe anything wrong, or yous want to share more data about the topic discussed to a higher place.


cartermcfaine.blogspot.com

Source: https://www.geeksforgeeks.org/constructor-chaining-java-examples/

Postar um comentário for "Can We Call Constructor Again After Updating Field Variables"