Why compiler does not provide default constructor ?

Why compiler does not provide default constructor if there is any parametrized constructor is provided by programmer.?

Showing Answers 1 - 3 of 3 Answers

sam505

  • 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
  1. class cube

  2. {

  3.         int length;

  4.         cube()

  5.         {

  6.                 length=10;

  7.         }

  8.         public void surface()

  9.         {

  10.                 System.out.println("Surface Area of  cube"+(6*length*length));

  11.         }

  12. }

  13.  

  14. class cube_cubeboid

  15. {

  16.         public static void main(String args[])

  17.         {

  18.                 cube c=new cube();

  19.                 c.surface();

  20.         }

  21. }

  22.  

  23.  


Output:

Surface Area of cube 600

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions