aspose file tools*
The moose likes Threads and Synchronization and the fly likes Simple question regarding runnable interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Simple question regarding runnable interface" Watch "Simple question regarding runnable interface" New topic
Author

Simple question regarding runnable interface

Roger Fed
Ranch Hand

Joined: Oct 17, 2010
Posts: 82

Hello
I've developed this program and running it

the program works well but i have some question:
in line 3, why we use the Runnable as the type of the created object???
I know that we can assign an object of class to another object as following
I've tried this and it works well
but i do not understand how can we assign an object of class to an interface!!!

so, what is the difference?
thanks in advance............

I Hear I Forget, I See I Remember, I Do I Understand.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18140
    
    8

Simple answer: you assign the instance to a Runnable variable so that you can use it in a context which requires a Runnable object. Of course that isn't necessary, since PrintChar implements Runnable anyway, but assigning it to a Runnable variable provides some useful information to future readers of the code. Namely that you intend to use the object as a Runnable object.
Roger Fed
Ranch Hand

Joined: Oct 17, 2010
Posts: 82

Paul Clapham wrote:Simple answer: you assign the instance to a Runnable variable so that you can use it in a context which requires a Runnable object. Of course that isn't necessary, since PrintChar implements Runnable anyway, but assigning it to a Runnable variable provides some useful information to future readers of the code. Namely that you intend to use the object as a Runnable object.

Thanks very much
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Simple question regarding runnable interface
 
Similar Threads
Properly Synchronizing a Block of Code
Need Explanation of unpredicted output even if methods are synchronized
Why this synchronization doesn't work
printing threads to a text area
using thread class to print to a text area