Let me try Case 1 - run method is overloaded -The run method in class a will never gets executed as the signature is different from the run method in class Thread. -So the code will compile and execute the do-nothing run method from class Thread.
Case 2 - run method is overridden -The program will compile and the run method in class a will get executed. Case 3 - Access modifier changed to be more private - Public to package access - Will not compile Case 4 - Should return an int - Compiler might give an error for no return statement Please let me know if these answers are correct. Thanks Sandeep
I would like to try--- case (1)-- run() should be defined with no parameters, so this code will result in compilation error case(2) -- Would run 0k. Output is "in run". case (3)-- not very sure - may not work because of access modifier!!! case(4) -- should return int?? Hope I did OK!!! PS: Rahul- your questions do help alot- thanks. Hope you keep posting questions for the beginners Neeraja [This message has been edited by Neeraja Rajan (edited August 29, 2000).]
posted 18 years ago
case 5 : Not a thread -Will print "in run" but in the same thread.
hello rahul, what i think is :-- case1:- run() method is overloaded not overridden. so program will compile after a.start() run method will not be called and neither overloaded run() is called in main() method so nothing will happen hence no output. case2:-- here Run() method is different from run() so run() method of thread class is not overidden. again this Run() method is not called in main(). so no error with no output. case3:-- access specifier of run() method is more restricted than it's super class . it will give a compiler error. case4:-- here signature of run() method is different because it is returning int, while run() method of thread class is void. so both are two different methods.again on error no output. please clarify me if i am wrong. gautam
posted 18 years ago
The answers are case 1 The run method is overloaded , compiler compiles and at runtime the default run of thread class is called and as such prints nothing case 2 Run is different from run. remember java is case sensitive so the code compiles but nothing is displayed as again the run method of Thread class is called which does nothing. case 3 raizes a compile time error. Note that run is declared as public void run(). By overriding this method by void run(); private void run(); protected void run(); would raize exceptions that cannot override run method of Thread class as the access specifier is reduced case 4 the compiler complains as the return type is different. Note that when you override a method the return type must be the same as that of the overriden method. Thus since run method of Thread class returns void the overriden method too should return void. case 5 The start() method of the Thread class has some functionality which is to create another thread and invoke the run method. when one overrides the start method then one does not create a new thread and instead calls the run method in the same thread. Check out the code below which prints the same name for the thread in which they are executing.
------------------ Regds. Rahul P. Mahindrakar
[This message has been edited by Rahul Mahindrakar (edited September 11, 2000).]