Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Simple question regarding runnable interface

 
Roger Fed
Ranch Hand
Posts: 82
Java Oracle
  • 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
Posts: 21319
32
Eclipse IDE Firefox Browser MySQL Database
  • 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
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic