File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A question for Beginners - 3

 
Rahul Mahindrakar
Ranch Hand
Posts: 1868
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Without compiling and given a demo class and cases 1 , 2 and 3 what are the likely outputs. Does the code compile?

public class demo {
public static void main(String args[]){
a first = new a();
first.start();
}
}
// case 1

// case 2

//case 3

// case 4

------------------
Regds.
Rahul P. Mahindrakar
[This message has been edited by Rahul Mahindrakar (edited September 11, 2000).]
 
Sandeep Potnis
Ranch Hand
Posts: 39
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mapraputa Is
Leverager of our synergies
Sheriff
Posts: 10065
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
remark:
case2: run() method is not overriden, class "a" has different method - Run(). It will compile, but probably Thread's run() method will be called, I am not sure.
 
Rahul Mahindrakar
Ranch Hand
Posts: 1868
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what happens in case 5. How is it different in execution.
 
Neeraja Rajan
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
Sandeep Potnis
Ranch Hand
Posts: 39
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
case 5 : Not a thread
-Will print "in run" but in the same thread.

Thx
Sandeep
 
sanjay gautam
Ranch Hand
Posts: 35
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rahul Mahindrakar
Ranch Hand
Posts: 1868
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic