File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Simple question regarding runnable interface

 
Roger Fed
Ranch Hand
Posts: 82
Java Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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............
 
Paul Clapham
Sheriff
Pie
Posts: 20177
25
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 82
Java Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic