• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Interface doubt

 
Greenhorn
Posts: 1
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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."

 
Ranch Hand
Posts: 492
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
reply
    Bookmark Topic Watch Topic
  • New Topic