Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A Question for beginners - 2

 
Rahul Mahindrakar
Ranch Hand
Posts: 1868
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Without compiling what is the result of executing the following program. why??
I recommend you not see others replies and post your replies.

Regds.
Rahul P. Mahindrakar
 
V Srinivasan
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code will not get compiled.
Because...
To create a Thread class should either extend Thread class or implement Runnable interface.
Thanks & Regards
 
vidya
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi!
Rahul, this is a really nice thing you do.... I think it really helps beginners like me...Thanks a lot...
About the code,
Well, the class demo1 doesnt implement the Runnable interface,
and the argument passed to the constructor new Thread(p) must be an object that implements Runnable.The compiler will point out that.(but I am not sure what exactly the error message will be..)
Or, the class demo1 should extend the Thread class so the method run() is available to it by inheritance...
Could you let me know what the error message will be?
thanks again...
Vidya.

 
Rahul Mahindrakar
Ranch Hand
Posts: 1868
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I made this post to make the point that implementing all the methods in the interface does not mean implementing the interface. In the code implementing the run method does not mean that one implements the Runnable interface. thus
Code 1:

is not the same as
Code 2:

Also the thread constructor not only takes a Runnable interface as a prarameter but can also take a Thread object as a parameter . for example

this is possible as the thread class itself is defined as
public class Thread extends Object implements Runnable
and since Thread class itself implements Runnable it can be passed as a parameter to the Thread constructor.
Vidhya,
Thanks for the complements. Complements really motivate.

Regds.
Rahul P. Mahindrakar


[This message has been edited by Rahul Mahindrakar (edited August 21, 2000).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic