CONSTRUCTORS
These are special member function of a class which is used to initialize the object. And their name is similar to the class name. Also they do not have any return type.
What is initialization?
> Initialization is to enter the first value for any parameter when it is born.
Why we use constructor for initializing values if we can do this with functions?
> It is because constructors are always executed if the object is created. But in case of functions, they are executed when called by the programmer.
> To understand this problem let us consider this program.
classEmp
{
intsalary;
voidget()
{
salary=15000;
}
}
In this program we are trying to initialize the value of salary with get() method. But this code has some problems:
· The get() function executed very late and sometimes don’t even executed. (it is not a necessary function)
· get() function can be called n numbers of times. So initialization n numbers of times cannot happen.
This is why we use Constructors to initialize the value of different types of variables.
Now there are some assumptions to define a constructor:
· It can be called only once and user cannot call it explicitly.
· According to the rule we define the function once the object is created and to get the name of class for object, the name of constructor is taken same as the name of class name.
· Constructors returns a value(it returns current class instance) implicitly therefore we cannot use return in constructors.
Types of Constructor:
Constructors are of two types:
· Parameterized constructor
· Non- Parameterized constructor (Default constructor)
1. Default Constructor (Non-Parameterized Constructor):
A default constructor do not have any argument. Let us try to understand this with the help of an example:
classTemp
{
intx;
inty;
Temp() //default constructor defined
{
x=20; //values initialized for variable x
y=20; //value initialized for variable y
}
voidshow() //function show() defined
{
System.out.println(x);
System.out.println(y);
}
publicstaticvoidmain(String[]s)
{
Temp t1=newTemp(); //we created an object of class Temp
t1.show(); //calling the method here
}
}
Points To Remember:
· we use default constructor when we want to keep the values of all data members same.
· whenever we wants to initialize the data member of same value then always use a default constructors and this concept is known as static initialization of non static data members.
Parameterized Constructors:
Whenever we wants to initialize the data member of each object with a different value then always use a parameterized constructors and this concept is known as dynamic initialization of non static data members.
An Example of Parameterized Constructor:
classTemp()
{
intx;
inty;
Temp(intx,inty) //parameterized constructor defined with two parameters
{
x=20;
y=20;
}
voidshow()
{
System.out.println(x);
System.out.println(y);
}
publicstaticvoidmain(String…s)
{
Temp t1=newTemp(10,20); //object created for Temp class and
t1.show(); //values of data members initialized
} //via Temp(10,20)
}
Constructor Chaining
Calling one constructor from another constructors with respect to current object is known as constructor chaining.
Whenever we wants to perform a different different task in a single constructor, rather than putting the code of each task into a single constructor, make a separate constructor for each task and then create their chain.
POINTS TO REMEMBER
· whenever we are achieving constructor chaining then this() must be the first line of constructor.
· whenever we are achieving constructor chaining using this() then there must be having one constructor without this(). Or which does not have this() as a first argument in a constructors.
· Constructor chaining can be achieved in any order.
Example of Constructor Chaining:
classConstructorchaining
{
Constructorchaining(intx,inty)//constructor with two parameters
{
this("Hello java");
System.out.println((x+y));
}
Constructorchaining(intx) //constructor with one parameter
{
this(10,20);
System.out.println(x);
}
Constructorchaining(Stringl)
{
System.out.println(l+" Function with constructor name");
}
Constructorchaining() //default constructor
{
this(10);
System.out.println("normal constructor");
}
publicstaticvoidmain(String...s)
{
newConstructorchaining();
}}
OUTPUT
Hello java function with constructor name
30
10
normal constructor
30
10
normal constructor
No comments:
Post a Comment