File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread Question from Dan's Mock exam Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Thread Question from Dan Watch "Thread Question from Dan New topic
Author

Thread Question from Dan's Mock exam

vydhehi paruchuri
Greenhorn

Joined: Aug 27, 2003
Posts: 22
This is a question from Dan's Mock exam.

class A extends Thread {
public A(Runnable r) {super(r);}
public void run() {System.out.print("A");}
}
class B implements Runnable {
public void run() {System.out.print("B");}
}
class C {
public static void main(String[] args) {
new A(new B()).start();
}}

According to my understanding since we are passing an instance of B as Runnable target when we call start() method "B" should be printed but the answer is "A".

Could someone please explain me?

Thanks

Vydhehi
Tom Tolman
Ranch Hand

Joined: Sep 02, 2004
Posts: 83
From the java API start()Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.

Thus, it will invoke the Threads run method. The Thread A has two potential run methods- the base class which will call start on Runnable B which prints B, and the derived class A which will call the run which prints A.

Which of these two methods will be invoked when the start method is called? It is bound dynamically, and actually is of type A, not Thread, so binds to the A method, and prints A.

Consider these as well:


This will print B.



If you comment out this line:


Then B is printed. It is no longer being dynamically bound- the run method is not overridden.
[ September 24, 2004: Message edited by: Tom Tolman ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread Question from Dan's Mock exam
 
Similar Threads
question from Dan's on thread
question of threads from Dan's mock
another of Dan's question
Threads question from Dan's Topic Exam 4
Dan's Thread question...