File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Interface doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Interface doubt" Watch "Interface doubt" New topic
Author

Interface doubt

Gaurav Ag
Greenhorn

Joined: Mar 19, 2012
Posts: 1

I always have a tough time understanding Interfaces.

As per my understanding Interface provides a contract between set of existing classes (or API) and our newly created classes. I understand that we can honor the contract by implementing the methods of interface, but I am still not sure how does it communicate with the other side (i.e. existing classes).

Let's take an example of Runnable interface. I do know that all the Thread methods (like start() method) goes into Thread class. So if in my class, I am implementing the run() function, how it is going to call the method of Thread class. As per my understanding, a very basic definition of Runnable interface should be something like -


So how overriding the run() method will actually call the methods in Thread class?

I know I am missing some basic concept here :-(.

Regards,
Gaurav
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Gaurav Ag wrote:Let's take an example of Runnable interface. I do know that all the Thread methods (like start() method) goes into Thread class. So if in my class, I am implementing the run() function, how it is going to call the method of Thread class.


Your Runnable doesn't call the methods of the Thread class. Or at least, it doesn't have to. It can if it needs Thread's functionality, like interrupting itself or another thread.

As per my understanding, a very basic definition of Runnable interface should be something like -


So how overriding the run() method will actually call the methods in Thread class?

It doesn't, unless you choose to because that's what you need to do to make your runnable work.

Rather, the thread calls the Runnable's run() method. That's the whole point. Thread is saying, "You give me a Runnable, and I'll call its run() method in a new thread of execution."

Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

There are two ways to make custom thread classes in Java:

1) Have your class implement the Runnable interface
2) Have your class extend the Thread class

Option 1 is the best option in my opinion because not only can your custom class run in its own thread, it can also choose to inherit from some other class if needed. This is nice since in Java multiple inheritance is not supported.

When you make your class extend Runnable, you aren't actually making a thread class, you are making a class that can be passed to a constructor of the java.lang.Thread class, namely the one that takes a Runnable as a parameter. After creating your Thread object using this Runnable you just call the thread's start() method, just like you would any other thread.

example:



Hope this helps.


"If the facts don't fit the theory, get new facts" --Albert Einstein
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Hunter McMillen wrote:There are two ways to make custom thread classes in Java:

1) Have your class implement the Runnable interface
2) Have your class extend the Thread class

Option 1 is the best option in my opinion because not only can your custom class run in its own thread, it can also choose to inherit from some other class if needed. This is nice since in Java multiple inheritance is not supported.


Option 1 is better, yes, but not for that reason, IMO. It's fairly uncommon to need your Runnable to extend some other class, and even if MI were supported, having a class that extends Thread and something else would be just icky.

The reason to prefer option 1 is simpler than that, and it's a design issue: You're not making a specialized kind of Thread so you shouldn't be extending Thread. You're creating a task to run in its own thread, which is what Runnable was created for.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Interface doubt