A. Prints : MyThread: start() followed by MyRunnable:run() B. Prints : MyThread: run() followed by MyRunnable:start() C. Prints : MyThread: start() followed by MyRunnable:start() D. Prints : MyThread: run() followed by MyRunnable:run() E. Compile time error F. None of the above
Can someone please explain in detail why the answer is A instead of D? Thanks a lot in advance!
Basically, the MyThread class overrides the start() method. When you call the start() method, it prints the start message. It doesn't actually start a thread that calls the run() method -- that code has been overridden.
The answer should be A MyThread class overrides the start() method. So when myThread.start() is called it calls the overridden start() method in MyThread class and not the start() method of Thread class beacsue the reference variable type is MyThread. Hence it will print MyThread: start() MyRunnable class does not override start() method. Its just any other method. Now Thread thread = new Thread(myRunnable); creates a new thread from myRunnable. Calling thread.start(); will look for Thread class' start method beacsue the reference variable type is Thread which would eventually try to call MyRunnable's run method.Hence it will print MyRunnable: run()
They worship nothing. They say it's because nothing lasts forever. Like this tiny ad: