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.
1 2 3 4 |
Person(int x, int y) { } |
If we want to send three values to constructor like Person r=new Person(15,25,35); then the constructor should be like
1 2 3 4 |
Person(int x, int y, int z) { } |
We can have any number of constructors in a class but we should see their arguments are different.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
class Person { Person(int x,int y) { Some code; } Person(int x,int y,int z) { Some other code; } } class Check { public static void main(String arg[]) { Person r1=new Person(); // NOT-VALID as there is no default constructor Person r2=new Person(10,20); // constructor with 2 arguments is called Person r3=new Person(15,25,35); // constructor with 3 arguments is called } } |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
class Person { Person () //default constructor { } Person(int x) //an argumented constructor { } } class Constructor { public static void main(String arg[]) { Person r1=new Person (); //default constructor is executed Personrp2=new Person (21); //argumented constructor is executed } } |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
class Square { int a; Square(int x, int y) { //Some code; } Square(Square s) { //some code } } class Person { int l,b; Person(Person r) { //some code } Person(Square s) { //some code } } class Check { public static void main(String arg[]) { Square s1=new Square(5); // Square(int) is called Square s2=new Square(s1); // Square(Square) is called Person r1=new Person(s1); // Person(Square) is called Person r2=new Person(r1); // Person(Person) is called Square s3=new Square(r1); // INVALID as there is no constructor in Square to receive a Person object as argument. } } |
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:
1 2 3 4 5 6 7 8 9 10 11 12 |
class Person { int l,b; Person() { this(10,20); } Person(int x, int y) { // some code } } |
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:
1 2 3 4 5 6 7 8 |
Person() { this(10); } Person(int x) { this(); } |
The this() should be the first statement if it exists. So the following code is invalid.
Example:
1 2 3 4 5 |
Person() { System.out.println("A"); this(10); //not allowed } |
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:
1 2 3 4 5 |
A() { System.out.print("A"); super(); // not valid } |
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:
1 2 3 4 5 |
B(int x) { super(); this(); //not valid } |
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:
1 2 3 4 5 6 |
class Sub extends Sup { Sub() { } } |
Example 2:
1 2 3 4 5 6 7 |
class Sub extends Sup { Sub() { super(); } } |
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:
1 2 3 |
Person r=new Person(); // calls default constructor Person s=new Person(10,20); // calls argumented constructor |
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
1 2 3 |
Person() { } |
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:
1 2 3 4 5 6 7 |
class Person { Person() // default constructor created by us { Some code; } } |
So, a class can have only one default constructor.