Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in K&B pg:167,Q:7

 
Sekhar Choudary
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI,
there is a question in K&B book that asks for inserting code that will make the program compile successfully.
Usaully, we can do the program to compile in two ways, this means, in two ways we can fix the code.

but according to KB book, th answer is like this

class AgedP{
AgedP(){ }
public AgedP(int x){
}
}
public class kinder extends AgedP{
public kinder(int x){ super(); }
}

But the following will also result in compilation successful, regardless of runtime exceptions(since the question is to make sure the program compile successfully.)

class AgedP{
AgedP(){ }
public AgedP(int x){ this(); //Newly inserted statement
}
}
public class kinder extends AgedP{
public kinder(int x){ super(); }
}

This will also compile successfully.

Which to select.

Thanks.
 
Harshit Rastogi
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you paste what is the original code in the question ?
 
Ga�lle Fournier
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I remember this question. I actually gave the answer with the "this();" statement.

It didn't phase me at the time because I wrongly read in my head "Fragments: Use the following fragments zero or one times". But looking back, it seems to me that the answer with the statement "this();" added is also correct bescause it actually says "Fragments: Use the following fragments zero or more times".

I would also like to know if it is correct or not.

PS : is it legal to post the question ?
[ July 26, 2008: Message edited by: Ga�lle Fournier ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic