• Post Reply Bookmark Topic Watch Topic
  • New Topic

master exam question  RSS feed

 
sweety singh
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

the answer of this question is Compilation fails... and the explanation says "Uber's constructor's call to this is looking for a no- argument constructor that doesn't exist"... i have not understood this... i have got confused please explain me this...
 
Antonio Tercero
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Compilation fails cause Uber doesn't declare the no-arg constructor
Uber(){}.
A call to this() is a call to the no-arg constructor.

Remember the follwoing rules:

If you don't type a constructor into your class code, a default constructor will be automatically generated by the compiler(the no-arg constructor).

If you want a no-arg constructor and you've typed any other constructor into your class code, the compiler won't provide the no-arg constructor.
 
sweety singh
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the reply... i wanted to ask one more thing...
in the following code... when the minor class's constructor calls its base class constructor with arguments.. ie super(y) at line 1.. is it that 'y +=3' also executes or before it is executed the base class constructor is called.??? then when is it executed.... same is the case with this()..

[ June 08, 2008: Message edited by: sweety singh ]
 
sweety singh
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
anyone please answer this question...
 
Swapnil Sonawane
Ranch Hand
Posts: 190
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please be patient Sweety.
 
Mustafa Musaji
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
y+=3 is executed after the base class constructor is called. If you comment out the line that says this(); and put run you will see what the result of y is and this should make it easier to understand what is happening.
 
Noam Wolf
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just another minor addition to remember for the exam is that if the super class has any Static init blocks or any instance init blocks then those would be called before the y += 3 statement.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!