20 Facts or Points to Master Constructor in Java


Java / Monday, July 29th, 2019

Let us first understand what is constructor with an example before discussing about 20 Facts or Points to Master Constructor in Java.

Constructors

A constructor initializes object with its creation. It has the same name as the name of its class. Once a constructor is defined, it is automatically called immediately the memory is allocated before the new operation completes. Constructor does not have any return type, it implicit return type is class object. You can see constructor as a class type. Its job is to initialize the instance variables of an object, so that the created object is usable just after creation.

In program Complex_Test you have seen that for initializing imaginary and real parts, two methods have been used. Both the methods have been invoked on the object one by one. In place of methods if you use constructor for initialization you have to make changes in Complex_Test program. Remove methods used for initializing variables and use one method having same name of the class, i.e., Complex with two arguments.

class Complex  { 
	double real; 
	double imag; 

	Complex( double p, double q) { 
		System.out.println("Constructor in process...");  
		real = p;
		real = p; 
		imag = q; 
		} 

	void showComplex ( ) { 
		System.out.println("The Complex Number is :"+ real +"+i"+imag); 
		} 
	} 

	class Complex_Test { 
		public static void main(String[] args)  
		{ 
			Complex R1 = new Complex(5,2); 
			R1.showComplex(); 
		} 
	} 

The output of this program is:

Constructor in process…
The Complex Number is :5.0+i2.0

 20 Facts or Points to Master Constructor in Java

1. A constructor is a special method which has no return value, not even void.

2. A constructor is usually a member function of a class.

3. The purpose of a constructor is to initialize the instances (objects) of the corresponding class.

4. When an object is created from a class, the variables of the class are initialized in each object. This is done by the constructor.

5. It is a method whose name is same as the class.

6. When no constructor is coded in a class, the compiler supplies one during run time. This is known as a default constructor.

7. The default constructor initializes all numeric variables to 0, all strings to ‘null’ value and all boolean variables to ‘false’ state.

8. Instances of class are created by the ‘new’ operator.

9. While creating objects of a class, the new operator automatically summons the special method known as the constructor for initialization of the variables of the object concerned.

10. The ‘this’ keyword refers to the current object.

11. A class may contain no constructor or one or more than one constructor.

12. When a class contains more than one constructor, the names of these are same as the class.  But they differ in signature.

13. Possessing more than one constructor by a class is a phenomenon which is referred to as constructor overloading.

14. Usually, a constructor is public. A public constructor may be accessed by other classes, from outside the class in which it is defined.

15. A constructor by be private or protected. Such constructor cannot be called to initialize instances of other classes outside the class which includes a private/ protected constructor.

16. There are two types of constructors: (i) parameterized and (ii) non-parameterized.

17. A parameterized constructor is one that has argument (s).

18. A non-parameterized constructor has no argument.

19. The parameters of the constructor are used to initialize the state of the new object.

20. When a compiler supplies a constructor to a class by default, it is non-parameterized.

 

 Question 01

What is parameterized constructor? Give an example.

It is a constructor with one or more arguments.

// Example of parameterized constructor

public class clubMember
{
public clubMember ( int idNo, float subscription)   // constructor with arguments
{
idNo = 0;
subscription = 0.0;
}  // End of the constructor
:  // Other members
}

 Question 02

What is non-parameterized constructor? Illustrate.

A constructor having no argument is called a non-parameterized constructor.

// Example of non-parameterized constructor

class demo
{
int x;
float y;
string A;
boolean filmActor;

public demo ()  // Non-parameterized constructor
{
x = 0;
y = 0.0;
A = null;
filmActor = False;
}
.
.
.
:  // Other members of the class
}

 Question 03

Difference between constructor and a method of a class.

ConstructorMethod
It has the same name as the class.It has a name different from the class.
Its specific job is to initialize the variable of an object.Performs same operation.
There is no return type.It has some return type including void.
It is invoked by the new operator.It is invoked by the dot operator.

 

 Question 04

What is constructor overloading? Explain by an example.

Constructor overloading is the process of allowing a class to have more than one constructor, all sharing the name of the class, but differing in types and number of parameters.

Example:

class demoConsOverloading
{
int x, y, z;

public demoConsOverloading ()  // non-parameterized constructor
{
x = 0;
y = 0;
z = 0;
}

public demoConsOverloading ( int p)  // parameterized constructor
{
x = p;
y = p;
z = 0;
}

public demoConsOverloading ( int p, int q, int r)  // Another parameterized constructor
{
x = p;
y = q;
z = r;
}

public void consOverloadingVerify ()
{
demoConsOverloading obj1 = new demoConsOverloading();  // using default constructor
demoConsOverloading obj2 = new demoConsOverloading(10);  // using constructor 2
demoConsOverloading obj3 = new demoConsOverloading(10, 20, 30);  // using constructor 3
}
}

 

<< Previous     Next>>

;

Leave a Reply

Your email address will not be published. Required fields are marked *