File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Dan's Thread question... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Dan Watch "Dan New topic

Dan's Thread question...

Bharat Roy
Ranch Hand

Joined: Jul 01, 2004
Posts: 156

I was solving Dan's mock and got this kind of question :

The output was : A

Then I commented out overridden run() method of class A like below :

The output was : B.

My doubt:

(1) In my 1st code, I have given B's instance to the class A's constructor then why it didn't call B's run() and printed B.

(2) How come in the 2nd code it reached B's run. Why it didn't reach to B's run() in the first code.

Kindly clear my doubt.

marc weber

Joined: Aug 31, 2004
Posts: 11343

Consider the line, (new A(new B())).start();

When we create an instance of A and pass an instance of B to its constructor, we are creating a Thread object from Runnable B (using the call to super), but we're still calling start on Thread object A. And although we created our Thread object using Runnable B, the Thread object is A, and A has overridden Threads's run method.

If A did not override run, then the parent (Thread's) version of run would be called. In the API for Thread's run method, it states, "If this thread was constructed using a separate Runnable run object, then that Runnable object's run method is called; otherwise, this method does nothing and returns." Therefore, because A was constructed using a separate Runnable object, B's run method would be called.

In other words, Thread's run behavior is to use the Runnable's run method if a Runnable instance (B) is provided in the constructor. However, if run is overridden, then the overridden version of run (in A) is invoked rather than the version of run in the parent class Thread (which would invoke B's run).
[ September 14, 2004: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
Tom Tolman
Ranch Hand

Joined: Sep 02, 2004
Posts: 83
I believe the answer is polymorphism, although may be wrong.

Look at it this way, you are calling the thread constructor passing in a class which implements Runnable. This tells the thread class- we have a thread to run- <i>when you are asked to initiate a thread, you are going to invoke your own run method which will invoke the run method on this passed in class</i>. Italics indicate I am guessing actually.

Now you <i>also implement a version of public void run()</i>. This is an override. In a polymorphic call- it will invoke your version rather than the base class version.

In the first version, you have an overridden method. The dynamic binding occurs on the .start and it invokes the run() method. Which run? The base class run method, or the derive class run method? Well- you overrode it, and it actually is of that type, so it is calling the overridden version, and prints out A.

In the second version, you DO NOT have an overrridden method. There is no dymanic binding- it calls the only version of run it knows of. It calls in fact- the version which invokes run on the runnable interface you passed in. This prints out B.

I am guessing but it seems reasonable. The question really is- what exactly is going on within start?
marc weber

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Tom Tolman:
I believe the answer is polymorphism, although may be wrong.
I think you're correct, although my post above probably does a poor job of explaining.

Because A was created with a Runnable object B, Thread's version of run will invoke B's implementation of run. However, because the extending class (A) overrides run, Thread's implementation of run isn't invoked at all (and therefore B's version of run isn't invoked).

[ September 14, 2004: Message edited by: marc weber ]
I agree. Here's the link:
subject: Dan's Thread question...
It's not a secret anymore!