This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
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
Sheriff

Joined: Oct 14, 2005
Posts: 19727
    
  10

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’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
 
subject: Simple question regarding runnable interface
 
It's not a secret anymore!