/* related posts with thumb nails */

default constructor:

4 Default Constructor is a constructor that takes no arguments.

4 Default Constructor is compulsory when there are parameterized constructors and one or more objects have no arguments in the program.

4 Default constructor is always advised to avoid garbage values for data members because this is used for providing default values for data-members.

4 Generally, this is used for assigning default initialization of values for the properties of the object.

Example:

class point

{

private int x,y;

point() // default Constructor

{

x=y=0;

}

point(int xx, int yy)

{

x=xx;

y=yy;

}

}

class Test

{

public static void main(String as[])

{

point p1=new point(3,4);

point p2= new point();

}

}

Explanation: In the above example, there are two constructors: constructor without arguments (default constructor) and constructor with two arguments. Default constructor is called for the object “p2” and its “x” and “y” are initialized to “0” i.e. default value
Related Topics:

0 comments:

Post a Comment