a cuestion  
Author Message
nmacaya





PostPosted: 2003-11-5 10:25:00 Top

java-programmer, a cuestion Hello

Which is the utility of declaring a interface without methods in Java?

thanks
 
Andrew Thompson





PostPosted: 2003-11-5 10:37:00 Top

java-programmer >> a cuestion "Nibaldo Macaya" <email***@***.com> wrote in message
news:email***@***.com...
....
> Which is the utility of declaring a interface without methods in Java?

An interface can _specify_, but not _implement_
methods. They tell the programmer what the
programmer needs to code.

This way, anybody that might use the 'xyz.class'
that implements an interface, knows that they have
some basic functionality that they can call on.

As to why the Java gurus decided not to allow
the implementation in the interface, I am not sure.

It seems to come down to 'multiple inheritance'
(which is not allowed in Java). If I were to imlement
two interfaces, both of which had a 'toString()' method
implemented, which would the final class use?

[ If I hold my breath, somebody might give
a better answer before I turn blue ..probably. ]


 
VisionSet





PostPosted: 2003-11-5 10:39:00 Top

java-programmer >> a cuestion

"Nibaldo Macaya" <email***@***.com> wrote in message
news:email***@***.com...
> Hello
>
> Which is the utility of declaring a interface without methods in Java?
>

To give a univeral *type* that can be accepted no matter how it is
implemented.

You could right a class that requires certain types as parameters to its
methods or constructors.
eg:

Collection myCollection = new ArrayList();

new ArrayList(myCollection);

All you have to do is write a class that implements Collection and you can
pass it in to that constructor.

The Java library programmers didn't need to care how you do it, but that
ArrayList code can still be useful to you.

--
Mike W


 
 
Harald Hein





PostPosted: 2003-11-5 14:46:00 Top

java-programmer >> a cuestion "Nibaldo Macaya" wrote:

> Which is the utility of declaring a interface without methods in
> Java?

To define a type without the need to define a class. Most OO languages
don't separate between a type and a class. Java does to a certain
extend (the primitives spoil the fun a little bit).

Especially the OO concept of polymorphism is in fact defined as type
compatibility, not class compatibility. This distinction gets lost if
you use an OO language which does not separate between types and
classes.

The distinction is handy, because you are no longer
bound to a certain inheritance hierarchy if you need type
compatibility. There is no need that two type compatible classes are
derived from the same base classe. All you need it to implement the
same interface. In Java this is also often used to partly work around
the missing multiple inheritance feature of the language.
 
 
Darryl L. Pierce





PostPosted: 2003-11-5 21:13:00 Top

java-programmer >> a cuestion Nibaldo Macaya wrote:

> Which is the utility of declaring a interface without methods in Java?

To declare a type which has no particular operations. For example, the
Serializable interface defines a type (a serializable object) but no
methods since the concept was that this type doesn't perform operations but
instead can have operations performed *on* it.

Though, doing so is discouraged, according to _Effective Java_.

--
Darryl L. Pierce <email***@***.com>
Visit the Infobahn Offramp - <http://bellsouthpwp.net/m/c/mcpierce>
"What do you care what other people think, Mr. Feynman?"