A constructor is a special member method which will be called by the JVM implicitly (automatically) for placing user/programmer defined values instead of placing default values. Constructors are meant for initializing the object.
Based on creating objects in JAVA we have two types of constructors. They are default/parameter less/no argument constructor and parameterized constructor.
A default constructor is one which will not take any parameters.
Syntax:
Class <clsname>
{
clsname () //default constructor
{
Block of statements;
...................;
...................;
}
..............;
..............;
};
For example:
class Test { int a, b; Test () { System.out.println ("I AM FROM DEFAULT CONSTRUCTOR..."); a=10; b=20; System.out.println ("VALUE OF a = "+a); System.out.println ("VALUE OF b = "+b); } }; class TestDemo { public static void main (String [] args) { Test t1=new Test (); } };
Whenever we create an object only with default constructor, defining the default constructor is optional. If we are not defining default constructor of a class, then JVM will call automatically system defined default constructor (SDDC). If we define, JVM will call user/programmer defined default constructor (UDDC).
A parameterized constructor is one which takes some parameters.
For example:
class Test { int a, b; Test (int n1, int n2) { System.out.println ("I AM FROM PARAMETER CONSTRUCTOR..."); a=n1; b=n2; System.out.println ("VALUE OF a = "+a); System.out.println ("VALUE OF b = "+b); } };
class Test2 { public static void main (String k []) { Test t1=new Test (10, 20); } };
Whenever we create an object using parameterized constructor, it is mandatory for the JAVA programmer to define parameterized constructor otherwise we will get compile time error.
Overloaded constructor is one in which constructor name is similar but its signature is different. Signature represents number of parameters, type of parameters and order of parameters. Here, at least one thing must be differentiated.
For
Test t1=new Test (10, 20); Test t2=new Test (10, 20, 30); Test t3=new Test (10.5, 20.5); Test t4=new Test (10, 20.5); Test t5=new Test (10.5, 20);
Whenever we define/create the objects with respect to both parameterized constructor and default constructor, it is mandatory for the JAVA programmer to define both the constructors.
Note:
When we define a class, that class can contain two categories of constructors they are single default constructor and 'n' number of parameterized constructors (overloaded constructors).
Write a JAVA program which illustrates the concept of default constructor, parameterized constructor and overloaded constructor?
Answer:
class Test { int a, b; Test () { System.out.println ("I AM FROM DEFAULT CONSTRUCTOR..."); a=1; b=2; System.out.println ("VALUE OF a ="+a); System.out.println ("VALUE OF b ="+b); } Test (int x, int y) { System.out.println ("I AM FROM DOUBLE PARAMETERIZED CONSTRUCTOR..."); a=x; b=y; System.out.println ("VALUE OF a ="+a); System.out.println ("VALUE OF b ="+b); } Test (int x) { System.out.println ("I AM FROM SINGLE PARAMETERIZED CONSTRUCTOR..."); a=x; b=x; System.out.println ("VALUE OF a ="+a); System.out.println ("VALUE OF b ="+b); } Test (Test T) { System.out.println ("I AM FROM OBJECT PARAMETERIZED CONSTRUCTOR..."); a=T.a; b=T.b; System.out.println ("VALUE OF a ="+a); System.out.println ("VALUE OF b ="+b); } };
class TestDemo2 { public static void main (String k []) { Test t1=new Test (); Test t2=new Test (10, 20); Test t3=new Test (1000); Test t4=new Test (t1); } };
Note:
By default the parameter passing mechanism is call by reference.