GeekInterview.com
Series: Subject: Topic:
Question: 34 of 1118

why compiler does not provide default constructor ?

why compiler does not provide default constructor if there is any parametrized constructor is provided by programmer.?
Asked by: Interview Candidate | Asked on: Sep 9th, 2011
Showing Answers 1 - 1 of 1 Answers
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
  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

  
Login to rate this answer.

Give your answer:

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

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.