GeeCON Prague 2014*
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: 18570
    
    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
 
GeeCON Prague 2014
 
subject: Simple question regarding runnable interface