Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > What is an interface?

Reply
Thread Tools

What is an interface?

 
 
Thomas Weidenfeller
Guest
Posts: n/a
 
      07-06-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Someone please help me out, I've looked at 50+ books on Interfaces.
> I understand contracts etc. But how on earth does an Interface method
> do something like start a thread running? eg:
>
> class TryThreads implements Runnable
> {
> public void run() {
> for (int x = 1; x < 100; x++) { // runs a new thread


No, it doesn't. It CAN run in a thread - if you tell a thread to do so.
It doesn't do it by its own.

> } }
>
> Above is MY implementation of the Interface method run() (its not
> exactly right, no matter), and it will run in its own thread.


Only when you tell a thread to do this.

> But I
> have not written anything to do with running a thread, so how does it
> 'know' to do this special task? (running a thread)???


It doesn't. You have to tell a thread to run it.

> All I do is my
> OWN implementation of run() which is just a 'for' loop. How does it
> 'know' to run it in its own thread??


It doesn't. Didn't I say this before?


> According to every book I have
> read, an Interface only forces me to write my own method with the
> method name & params that is in the Interface. hat I understand, so how
> does it run a thread?


It doesn't. Didn't I ... )

> Its not in my implementation.
>
> Will some kind person explain whats going on in simple English that
> even I could understand.


A runnable represents a piece of code which can be run by someone.
Whoever wants to run that piece of code can run it by calling the run()
method.

You can run it yourself by calling the run() method.

If you tell no one to run it, it will not run.

If you tell a thread to run it (in parallel to your current thread), the
thread will run it by calling the run() method of the runnable:

Runnable codeToRun = new TryThreads();
Thread aThread = new Thread(codeToRun);
aThread.start(); // runs the code in a separate thread

"new Thread(codeToRun)" creates a new thread object, which is not
running at this point. But which already knows that the thread, when
started, should run the code in "codeToRun". The call to start() indeed
starts the thread, which will do what it has been told: run the
"codeToRun", by calling codeToRun.run().


/Thomas

--
The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/...g/java/gui/faq
http://www.uni-giessen.de/faq/archiv....java.gui.faq/
 
Reply With Quote
 
 
 
 
Chris Uppal
Guest
Posts: n/a
 
      07-06-2005
(E-Mail Removed) wrote:

> 'run() is used as a callback' this is the magic explanation that I have
> never understood or even seen in my 50 books.This is beginning to sound
> rational at long long long last. Why don't books mention 'callbacks' in
> Interfaces? It simply does not make sense without that (in my small
> mind). They only ever discuss 'contracts' which I understand
> completely.


Probably because interfaces and callbacks are completely separate beasts.

However there /is/ a connection of sorts. If you have some object, and you
want to invoke one of its methods, then you have to know that that method
exists. A callback is just a special case of invoking a method (there's no
formal difference, it's just that some methods are used as callbacks, some are
not). So, for the Java runtime machinery to issue the callback method run(),
it has to be sure that he method exists. That's where interfaces come in. The
interface is (among other things) a contract defining what methods the caller
can expect to find. The specific interface java.lang.Runnable, represents the
contract that any object that implements that interface will have a void run()
method. So, when you start a thread you (normally) provide an object that
implements java.lang.Runnable (which is checked by the compiler, and by the
runtime), so the system knows that it can safely invoke that object's run()
callback method as the body of the new thread.

-- chris



 
Reply With Quote
 
 
 
 
.
Guest
Posts: n/a
 
      07-06-2005
On Tue, 5 Jul 2005 (E-Mail Removed) wrote:

> Someone please help me out, I've looked at 50+ books on Interfaces.
> I understand contracts etc. But how on earth does an Interface method
> do something like start a thread running? eg:


You are mixing two things here. An interface is just a way of listing the
methods you want someone to create. For example, if your class implements
the Runnable interface you are required to have a public method called run
that takes no inputs and returns void. If you implement this interface you
have not created a thread. The creating of a Thread is something
COMPLETELY seperate.

> class TryThreads implements Runnable
> {
> public void run() {
> for (int x = 1; x < 100; x++) { // runs a new thread
> } }


The above class implements the interface Runnable. This does not make it a
Thread. I might create a project that requires a type called NotAThread.
My code is going to assume that the class NotAThread will have a method
called run that takes no inputs and returns void. Rather than create an
interface to enforce this requirement I see that there is an interface
called Runnable so I just use that. My project has nothing to do with
Thread but I can still use the Runnable interface.

> Above is MY implementation of the Interface method run() (its not
> exactly right, no matter), and it will run in its own thread.


Incorrect. Your above call has little to do with threads. On its own it
cannot run as a thread. All the magic to do that is in the Thread class.
The interface Runnable determines WHAT will become a thread. It does not
determine HOW something will become a thread.

> But I have not written anything to do with running a thread, so how does
> it 'know' to do this special task? (running a thread)??? All I do is my
> OWN implementation of run() which is just a 'for' loop. How does it
> 'know' to run it in its own thread?? According to every book I have
> read, an Interface only forces me to write my own method with the method
> name & params that is in the Interface. hat I understand, so how does it
> run a thread? Its not in my implementation.
>
> Will some kind person explain whats going on in simple English that
> even I could understand.


What you really want to understand is not interfaces but Thread.

You can create a thread by passing Thread a class that implements
Runnable.

The interface Runnable is just a contract between Thread and your class
promising that you have created a method called run that requires no
inputs and returns void. What Thread does with that method is the magic
that is threads.

The interface Runnable tells us WHAT method is going to be turned into a
thread. It does not tell us HOW it is turned into a thread.

Interfaces tell us WHAT methods are needed. It is a completely seperate
issue as to WHY those methods are needed and HOW they will be used.

--
Send e-mail to: darrell dot grainger at utoronto dot ca

 
Reply With Quote
 
billreyn@gmail.com
Guest
Posts: n/a
 
      07-06-2005
Thanks Thomas, its slowly starting to make sense. Its at the limits of
my intelligence though.

 
Reply With Quote
 
George Cherry
Guest
Posts: n/a
 
      07-06-2005

"Chris Uppal" <(E-Mail Removed)-THIS.org> wrote in message
news:42cbc574$0$38038$(E-Mail Removed).. .
> (E-Mail Removed) wrote:
>
>> 'run() is used as a callback' this is the magic explanation that I have
>> never understood or even seen in my 50 books.This is beginning to sound
>> rational at long long long last. Why don't books mention 'callbacks' in
>> Interfaces? It simply does not make sense without that (in my small
>> mind). They only ever discuss 'contracts' which I understand
>> completely.

>
> Probably because interfaces and callbacks are completely separate beasts.
>
> However there /is/ a connection of sorts. If you have some object, and
> you
> want to invoke one of its methods, then you have to know that that method
> exists. A callback is just a special case of invoking a method (there's
> no
> formal difference, it's just that some methods are used as callbacks, some
> are
> not). So, for the Java runtime machinery to issue the callback method
> run(),
> it has to be sure that he method exists. That's where interfaces come in.
> The
> interface is (among other things) a contract defining what methods the
> caller
> can expect to find. The specific interface java.lang.Runnable, represents
> the
> contract that any object that implements that interface will have a void
> run()
> method. So, when you start a thread you (normally) provide an object that
> implements java.lang.Runnable (which is checked by the compiler, and by
> the
> runtime), so the system knows that it can safely invoke that object's
> run()
> callback method as the body of the new thread.


Yes. Maybe this will also help the op: A Thread is
a worker; the run() method in an instance of type
Runnable defines the work or task that the worker
Thread performs. The run() method gives the job
(task) description for the worker Thread. Also, the
Runnable interface is a more flexible and versatile
way to define the work than subclassing Thread
in a class that defines run()--since Java has only
single inheritance for implementations.

George W. Cherry


 
Reply With Quote
 
Dale King
Guest
Posts: n/a
 
      07-15-2005
(E-Mail Removed) wrote:
> I still can't see what 'Runnable' gets you if it has no connection to
> the Threads class, why use it at all? There must be some message passed
> for it to make any sense?
>
> This program creates a new thread and start() calls run(). Why? There's
> no connection.


Yes, there is a connection. That connection does not come from the
Runnable interface itself. The connection is in this code:

> TryThreads tr = new TryThreads();
> Thread t = new Thread(tr); // t is a new thread called 'Fred'


You created a new thread and "connected" it to the TtyThreads instance.
The thread object you created saves the reference to the Runnable
instance in a field of the thread object.

When you invoke start() it creates a new thread which then gets the
reference to the Runnable instance and invokes its run method.

> Whats the Interface doing - is it that the Thread object
> looks for a method called run()? In which case why not just have a
> method called run() with no Runnable Interface, that should work too
> (but it doesn't)?


But there is no way in Java to reference a method except for reflection.
If you want to point to something else that something else has to be an
object.

Interfaces are somewhat like a function pointer, but it allows you to
point to a whole set of functions not just one.

I could confuse the matter further by telling you that you can also
subclass Thread and put your run method in the subclass. This only works
because, in a very bad design decision on Sun's part, Thread itself
implements Runnable and the no-arg constructor stores a reference to
itself. I recommend against this approach however.

--
Dale King
 
Reply With Quote
 
Andrew McDonagh
Guest
Posts: n/a
 
      07-15-2005
Dale King wrote:
> (E-Mail Removed) wrote:
>
>
> Interfaces are somewhat like a function pointer, but it allows you to
> point to a whole set of functions not just one.


Interfaces are nothing like a function pointer.

They are a Type-ing mechanism, much like a C++ pure abstract class, only
with greater restrictions with which they can contain.

Andrew
 
Reply With Quote
 
Mike Schilling
Guest
Posts: n/a
 
      07-15-2005
"Andrew McDonagh" <(E-Mail Removed)2s.com> wrote in message
news:db90m3$ndd$(E-Mail Removed)2surf.net...
> Dale King wrote:
>> (E-Mail Removed) wrote:
>>
>>
>> Interfaces are somewhat like a function pointer, but it allows you to
>> point to a whole set of functions not just one.

>
> Interfaces are nothing like a function pointer.
>
> They are a Type-ing mechanism, much like a C++ pure abstract class, only
> with greater restrictions with which they can contain.


An interface in Java is a contract to implement a set of method signatures,
nothing more and nothing less.


 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      07-15-2005
"Mike Schilling" <(E-Mail Removed)> writes:
>An interface in Java is a contract to implement a set of method
>signatures, nothing more and nothing less.


It is a (reference type) specification, which might be used in
a declaration, instanceof, or cast expression.

A contract is an act resulting from the corresponding
declaration of two parties. What would correspond to such a
declaration would not be the interface( specification), but
the "implements" clause of a class declaration. This alone is
not a declaration from two parties, but from a single party.

Then, a reference expression might be written, which has an
interface type, which then indeed comes close to a kind of
contract between a class and a client.

 
Reply With Quote
 
Andrew McDonagh
Guest
Posts: n/a
 
      07-15-2005
Mike Schilling wrote:
> "Andrew McDonagh" <(E-Mail Removed)2s.com> wrote in message
> news:db90m3$ndd$(E-Mail Removed)2surf.net...
>
>>Dale King wrote:
>>
>>>(E-Mail Removed) wrote:
>>>
>>>
>>>Interfaces are somewhat like a function pointer, but it allows you to
>>>point to a whole set of functions not just one.

>>
>>Interfaces are nothing like a function pointer.
>>
>>They are a Type-ing mechanism, much like a C++ pure abstract class, only
>>with greater restrictions with which they can contain.

>
>
> An interface in Java is a contract to implement a set of method signatures,
> nothing more and nothing less.
>
>


I think you are taking a simplistic meaning from what is a woolly
training wording of 'interfaces as contracts to implement' .

Yes, a class implementing an Interface is bound to either implement or
mark as abstract the method signatures defined within the interface.
However they are first and foremost a typing mechanism.

If it wasn't a type specification, how could we pass objects around as
either their direct class type OR the interface they implement?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments