• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

K&B Question Chapter 2

 
Jay Pawar
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the question from K&B book chapter 2

Which two statements are true for any concrete class implementing the
java.lang.Runnable interface? (Choose two.) ?

A. You can extend the Runnable interface as long as you override the public run() method.
B. The class must contain a method called run() from which all code for that thread will be initiated.
C. The class must contain an empty public void method named run().
D. The class must contain a public void method named runnable().
E. The class definition must include the words implements Threads and contain a method called run().
F. The mandatory method must be public, with a return type of void, must be called
run(), and cannot take any arguments.


The answer given is B & F

The explanation given in the book is as follows

When a thread´┐Żs run() method completes, the thread will die. The run()
method must be declared public void and not take any arguments.
A is incorrect because classes can never extend interfaces.
C is incorrect because the run() method is typically not empty; if it were, the thread would do nothing.
D is incorrect because the mandatory method is run().
E is incorrect because the class implements Runnable.


I understand why F is correct, however I am not understanding why B should be correct. The option B says about method run is needed, however does not specify about the access modifier.

On the contrary option C, I feel is correct because it does specify the run method to be public ( which is mandatory condition ). Option C does say about the run method being empty, which I feel should be fine because the question doesn't seem to test on the implementation part of the overridden run method.

Guys, do post your views, I might be missing here something.

TIA
[ December 18, 2004: Message edited by: Jay Pawar ]
 
Joe Borderi
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand your quibble with choice B; however, choice C is wrong because it reads "The class must contain an empty public void method named run()" (bold and italics added). If must must were replaced with may, choice C would be correct. The explanation given by the author gives the reason that a programmer shouldn't and not a reason that a programmer can't.

Returning to choice B ("The class must contain a method called run() from which all code for that thread will be initiated."), I read the statement as stipulating a necessary but not sufficient condition.

Reflecting upon the choices, B & F are the best two, per the question's hint, choices. If, however, choice C were rewritten, I believe C would be a better choice than B.
 
Jay Pawar
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the detailed explanation. Got to be watchful with those words.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic