➧What is Constructor ?
Constructor Java Source Program has a special type of method or function. When the creator creates a constructor in his Java Source program, he keeps his name the same as what he named his class.
To identify the constructor, it will be necessary to see that the name of the name in the source program is also the function of its name. If both have the same name and there is no return type of that function then that function will be called constructor. Before using Object, if you want to perform a Task, such as initializing the variables, creating an Array, creating a string etc. | You can do all this work by creating Constructor.
Syntax :
Creating Constructor :
How to Create Constructor in Java We can easily understand this from a Java's Source Program -
Source Code :
OUTPUT : Name : Ashish Singh
Phone : 147258369
Email : Ashish 123@gmail.com
Read Also :
- Constructor overloading in java with examples full details.
Property of Constructor :
Constructor also has some property on which the constructor can be identified, some constructor's property is as follows:
- The name of the constructor is the name of the class, which means that the name of the class is the name of the same constructor.
- Constructor is a member function of your class.
- Constructor is also like a method.
- Constructor does not have a return type, so you can not type its Return Type.
- Class Data Members are initialized using Value Constructor.
- Constructor does not have to call It automatically gets called as the object of the class is created.
- Constructor is not virtual because it is declared in a public section.
- Constructor can not be inherited by Derived Class.
- If the name of the constructor is not written in the name of the class, then we call it Function or Method, we can not call it Constructor.
- Constructor is automatically called Call as often as the object creates.
Types of Constructor :
Constructor in Java is defined in three different ways. The three types of Constructor are the following:
- Default Constructor ( Non – Parameterized Constructor ).
- Parameterized Constructor.
- Copy Constructor.
1. Default Constructor :
While creating Default Constructor, the programmer does not present any argument in the Default Constructor. By Default JVM understands this as the default constructor. Default Constructor is also called Non-Parameterized Constructor and Empty Constructor.
OUTPUT : Value of a is : 101
Value of a is : 102
Value of a is : 103
In this program there is a class named B, in which some variables have been declared. After this, Constructor has been Declare within Class named B. This constructor has been asked to print the value of variables inside. Now we have created the object of this class by creating the main method. Constructor of this class will automatically become Call as soon as the object is created and in the OUTPUT the value of a, b, c will be printed.
2. Parameterized Constructor :
In the constructor we can also pass Parameters (Argument), so it is called Parameterized Constructor. Parameters are passed in the Parameterized Constructor that, the programmer holds the value given at the time of Creating the Object, and also asigns the Value in the created variables along with it.
When creating an object in Parameterized Constructor, the value of Parameters is mandatory. Let us understand this better through an example.
Source Code :
In this program a class named B has been created and some Datatype Declare has been made in it. The Constructor of B Class has been Declare and its Constructor has passed the Argument of two Integer Types. This argument holds the Integer value given by Programmer while creating the Object. After this, a show () name has been created in which a and b has been asked to print a multiplication. After that, the object has been created by creating the mail method of this class and then the method has been called.
OUTPUT : Multiplication of a and b is : 25
In this program a class named B has been created and some Datatype Declare has been made in it. The Constructor of B Class has been Declare and its Constructor has passed the Argument of two Integer Types. This argument holds the Integer value given by Programmer while creating the Object. After this, a show () name has been created in which a and b has been asked to print a multiplication. After that, the object has been created by creating the mail method of this class and then the method has been called.
3. Copy Constructor :
Copy constructor is used only to copy the value. Its concept is that two constructors are created inside the class in its source program and in that same constructor, the value of a constructor is copied to the constructor of the second. In this, after creating the object of a class, the object of the object is passed in the form of Argument. Let us understand this easily through an example.
Source Code :
Thank You !
OUTPUT : Multiplication of a and b is : 25
Read Also :
- Constructor overloading in java with examples full details.
Like this post, If you find Topic useful in this post, so share it with your friends and colleagues on Facebook, Twitter, whatsapp and Google Plus.
Thank You !
Note – For More Core & Advance Java Concepts , Tutorials And Programs Are Available in www.kptechsolution.com
0 Comments