Java Programming - Threads - Discussion
Discussion Forum : Threads - Finding the output (Q.No. 3)
3.
What will be the output of the program?
class MyThread extends Thread
{
MyThread() {}
MyThread(Runnable r) {super(r); }
public void run()
{
System.out.print("Inside Thread ");
}
}
class MyRunnable implements Runnable
{
public void run()
{
System.out.print(" Inside Runnable");
}
}
class Test
{
public static void main(String[] args)
{
new MyThread().start();
new MyThread(new MyRunnable()).start();
}
}
Answer: Option
Explanation:
If a Runnable object is passed to the Thread constructor, then the run method of the Thread class will invoke the run method of the Runnable object.
In this case, however, the run method in the Thread class is overridden by the run method in MyThread class. Therefore the run() method in MyRunnable is never invoked.
Both times, the run() method in MyThread is invoked instead.
Discussion:
17 comments Page 2 of 2.
Rahul said:
9 years ago
The correct option is c.
Because Runnable is abstract it can not be instantiated so this new MyThread(new Runnable()).start(); will not compile and throws a error.
Because Runnable is abstract it can not be instantiated so this new MyThread(new Runnable()).start(); will not compile and throws a error.
Afzal khan said:
2 years ago
The right Answer is ->Prints "Inside Thread Inside Runnable" not the ->Prints "Inside Thread Inside Thread".
Please correct it.
Please correct it.
(1)
David said:
7 years ago
It will give the compilation error. Because you can not call super with an argument.
MyThread (Runnable r) {super (r);}.
MyThread (Runnable r) {super (r);}.
Charu said:
1 decade ago
Could anyone suggest why the answer is A for this. As not satsifed by the answer.
Chintha said:
1 decade ago
If MyThread class doesn't override the run method, the output will b A.
Saurabh vyas said:
1 decade ago
i got the solution it was a good question. the answer is correct
Sriram said:
1 decade ago
See the run() method in MyRunnable is never invoked.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers