wood burning stoves*
The moose likes Beginning Java and the fly likes interface(runnble) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "interface(runnble)" Watch "interface(runnble)" New topic
Author

interface(runnble)

nethanjana indrakeela
Greenhorn

Joined: Nov 16, 2004
Posts: 2
hi all,

the runnable interface has only run methode signature,

we can use that after imlement,but why we use it like that?

why can't we use full run methode in our own class?
what is the deferent that ether interface and full methode in class?

Indarakeela
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

You certainly can write your own class with a "public void run()" method in it. This class would not implement the "Runnable" interface. However, what you cannot do is then use this class to create a Thread. Even though the class has the "run" method defined, when you create a Thread with a Runnable, it is expecting something of type "Runnable," as this is the only way to gaurentee that the "run" method exists in the class.

For example, suppose you mistyped "run" as "ryn". Without implementing Runnable, the compiler would compile happily. However, with Runnable, you would get an error message stating something to the effect of "Your class is incomplete -- you need to implement the "run" method." You look back, thinking But I did implement the run method and discover that it is misspelled. You correct the misspelling and everything compiles happily.

(No, this is not far-fetched. Someone here recently had this problem, albeit with the actionPerformed method in another interface -- which was spelled actionperformed -- without the capital 'P.')


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
As Joel said, there is nothing stopping you from creating a run() method without implementing Runnable. In fact, there is nothing special in and of itself about the run() method defined in Runnable. What makes it special is that the Thread class expects to find this method in order to call it. You can create a Thread object that uses another class that implements Runnable and the expected run() method. For more details about how this works, you should google or use the Saloon's search tool for something about threading in Java.

HTH

Layne


Java API Documentation
The Java Tutorial
 
Consider Paul's rocket mass heater.
 
subject: interface(runnble)
 
Similar Threads
Requirement of Interface in Java
interface
function overloading
Interface vs abstract class
singleton.clone()