sam505
Answered On : Sep 14th, 2011
1)Compiler does not provide default constructor in case when we call parametrized constructor,because default constructor does not contain any parameter,it is parameter less constructor.
2)Normally default constructor is always called when instance of the class(object) is initialized ,even when we does not create it and call it.We can also explicitly create the default constructor (without parameter) and call it.
3)Even if we does not call the created default constructor it will be called when the instance of the class is created.
Here is the simple example:
In default constructor I have initialized the value of instance variable.
Code
class cube
{
int length;
cube()
{
length=10;
}
public void surface()
{
System.out.println("Surface Area of cube"+(6*length*length));
}
}
class cube_cubeboid
{
public static void main(String args[])
{
cube c=new cube();
c.surface();
}
}
Output:
Surface Area of cube 600
Login to rate this answer.