Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > What's the use of an interface

Reply
Thread Tools

What's the use of an interface

 
 
Hakusa@gmail.com
Guest
Posts: n/a
 
      01-18-2007
I'm reading the http://java.sun.com/docs/books/tutorial/java/TOC.html
tutorial and I'm passed interfaces, but I can't figure out what's so
good about them. If I wanted to make all the methods, why don't I just
make them instead of implementing an interface that seems to add
nothing, but the necessary to make all the methods you would anyway?
Please rid me of my ignorance.

PS: I've also read about abstract classes for instead of interfaces,
but I think I'd rather make a static class with static methods.

 
Reply With Quote
 
 
 
 
Manish Pandit
Guest
Posts: n/a
 
      01-19-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> I'm reading the http://java.sun.com/docs/books/tutorial/java/TOC.html
> tutorial and I'm passed interfaces, but I can't figure out what's so
> good about them. If I wanted to make all the methods, why don't I just
> make them instead of implementing an interface that seems to add
> nothing, but the necessary to make all the methods you would anyway?
> Please rid me of my ignorance.
>
> PS: I've also read about abstract classes for instead of interfaces,
> but I think I'd rather make a static class with static methods.


Interfaces are used to decouple behavior. They are also used to expose
a contract that the "concrete" classes will have.Interfaces provide a
"type" to the concrete class (in a way, this is often refered to as
java's version of multiple inheritance).

To answer your question - why not just code all the methods then and
there instead of having a separate "piece of code" for the method
definitions - a part of it is about separation of behavior from the
exposed contract. In simple context, look at AWT event model. It has
interfaces for event listeners, so that *you* can code the *behavior*
any way you want it. There would not be (easy) remote/distributed
programming with Java if it were not for the interfaces.

I suggest you read up on object oriented design principals, and how
they are implemented/exposed in Java. This should give you a better
understanding of OO-practices.

-cheers,
Manish

 
Reply With Quote
 
 
 
 
Brandon McCombs
Guest
Posts: n/a
 
      01-19-2007
(E-Mail Removed) wrote:
> I'm reading the http://java.sun.com/docs/books/tutorial/java/TOC.html
> tutorial and I'm passed interfaces, but I can't figure out what's so
> good about them. If I wanted to make all the methods, why don't I just
> make them instead of implementing an interface that seems to add
> nothing, but the necessary to make all the methods you would anyway?
> Please rid me of my ignorance.
>
> PS: I've also read about abstract classes for instead of interfaces,
> but I think I'd rather make a static class with static methods.
>


For one thing, interfaces help get around the limitation (feature?) of
only allowing a class to extend 1 other class at a time. I mainly
consider that a feature although it can be annoying at times but I
usually find a way around it and I'm happy again.

Another thing is that when a class implements an interface the class
also becomes an instance of the interface type. So if you have an
interface called Date you could create a class called Gregorian that
implements Date you make Gregorian a Date class. So it works like
inheritance but you aren't limited to 1 interface like you are with real
inheritance.

Third, you are right in that an interface only defines method signatures
however there is a good reason for that. By using this feature in
conjunction with the last one I listed you can define the methods to do
work specific to the class that implemented the method.

For example: I have a program that has a JTabbedPane with 3 tabs. One
tab has a JList, the other 2 tabs each have a JTree on the left and a
JList on the right but they show different data. I created a class that
implements the Runnable interface to make it do work in a thread. The
results from the thread are then passed back to the tab that spawned the
thread. I wanted the thread class to be generic so it can be used
across any of the tabs but I wasn't sure how I could make sure the data
from a particular search was passed to the correct tab to be displayed.

I decided to make an interface that each of the classes that make up the
tabs would implement and pass the class that creates a tab's GUI as an
argument to the thread class. Since those 3 tab classes were considered
to be of type AsyncSearchInterface it was easy to define the thread
constructor to allow that. In the AsynSearchInterface interface I
specified a method called updateGUI() that would be called within the
AsyncSearch thread to give the tab its results. The individual tab
classes then define how they process the results. The usefulness of the
interface came in handy here because I could define in each of the tab
classes what I wanted each of them to do within updateGUI(). The first
tab needed to update its listModel but the other tabs had to update a
listModel (that wouldn't contain the same data as the other one) but
also a treeModel.

The interface method can do different things and the thread doesn't
care; that is the power of only declaring method signatures in the
interface without specifying their body definitions. This is even more
apparent if you have more than 1 class that can utilize the interface (I
had 3) otherwise you are correct, you could just define all the methods
in one of your classes. But if those methods need to be used elsewhere
but the work inside of them is different even though the classes have to
be treated the same then an interface will help immensely.


Those are the reasons I like interfaces. I'm sure there are more but
those are off the top of the head of a non-professional programmer (but
I develop for fun, yes, for fun).

Brandon
 
Reply With Quote
 
Hakusa@gmail.com
Guest
Posts: n/a
 
      01-19-2007
Ok, I think I will some day understand most of what you said. But at
least today, I learned the usefulness of interfaces. I guess I just
didn't really understand Java's tutorial well enough, but it makes more
sense now.

And again, I feel like less of an ignorant fool. Thanks for every reply.

 
Reply With Quote
 
jupiter
Guest
Posts: n/a
 
      01-19-2007

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> I'm reading the
> http://java.sun.com/docs/books/tutorial/java/TOC.html
> tutorial and I'm passed interfaces, but I can't figure out what's
> so
> good about them. If I wanted to make all the methods, why don't I
> just
> make them instead of implementing an interface that seems to add
> nothing, but the necessary to make all the methods you would
> anyway?
> Please rid me of my ignorance.
>
> PS: I've also read about abstract classes for instead of
> interfaces,
> but I think I'd rather make a static class with static methods.
>


Another behavior that I think is worth noting is that adding this
level of abstractness (interface) forces structure on other people
who work on your code. If you design code as an API instead of
just an implementation then you give people flexibility and
structure with interfaces.

I think none of it really matters much if you don't care if your
code is scalable or extendable or fair game for other programmers.
But if you ever want to scale it up by adding features you will
probably be glad that you provided the structure of the abstract
layer.

Let's face it, nobody writes an interface to do a Hello World, but
the possibility does exist of massive APIs that need structure, and
even need to force programmers to implement in a certain way.



 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      01-19-2007
jupiter wrote:
> Another behavior that I think is worth noting is that adding this
> level of abstractness (interface) forces structure on other people
> who work on your code. If you design code as an API instead of
> just an implementation then you give people flexibility and
> structure with interfaces.
>
> I think none of it really matters much if you don't care if your
> code is scalable or extendable or fair game for other programmers.
> But if you ever want to scale it up by adding features you will
> probably be glad that you provided the structure of the abstract
> layer.


Google "Inversion of Control" and "Dependency Injection". The Spring framework,
<http://www.springframework.org>
, uses these principles, for example.

- Lew
 
Reply With Quote
 
Steve W. Jackson
Guest
Posts: n/a
 
      01-19-2007
In article <(E-Mail Removed) m>,
"(E-Mail Removed)" <(E-Mail Removed)> wrote:

> Ok, I think I will some day understand most of what you said. But at
> least today, I learned the usefulness of interfaces. I guess I just
> didn't really understand Java's tutorial well enough, but it makes more
> sense now.
>
> And again, I feel like less of an ignorant fool. Thanks for every reply.


IMHO, Java's tutorial is pretty good for learning *how* to do many
things in Java. But it's not a good source of knowledge on the language
itself. A Java language tutorial or book is where you would learn more
about what interfaces are and why, among countless other things. And
combining that with good information on OO principles will make it even
better.

= Steve =
--
Steve W. Jackson
Montgomery, Alabama
 
Reply With Quote
 
Tarkin
Guest
Posts: n/a
 
      01-19-2007

Steve W. Jackson wrote:
> In article <(E-Mail Removed) m>,
> "(E-Mail Removed)" <(E-Mail Removed)> wrote:
>
> > Ok, I think I will some day understand most of what you said. But at
> > least today, I learned the usefulness of interfaces. I guess I just
> > didn't really understand Java's tutorial well enough, but it makes more
> > sense now.
> >
> > And again, I feel like less of an ignorant fool. Thanks for every reply.

>
> IMHO, Java's tutorial is pretty good for learning *how* to do many
> things in Java. But it's not a good source of knowledge on the language
> itself. A Java language tutorial or book is where you would learn more
> about what interfaces are and why, among countless other things. And
> combining that with good information on OO principles will make it even
> better.
>
> = Steve =
> --
> Steve W. Jackson
> Montgomery, Alabama


Not to be critical, but, to paraphrase you:
Java's tutorial...not a good source of knowledge on the language
itself.

and then:
A Java language tutorial...is where you would learn more
about what interfaces are and why, among countless other things.

I don't fully disagree (reading the tutorial felt like eating a
doughnut
for dinner), but what does that say to newcomers (including myself),
that the people who *wrote* the language cannot create a satisfactory
tutorial?

And some books aren't much better- I have a Sybex Java 2 "Complete"
(IIRC, it was written not long after 1.3 was released) that glossed
over a lot information, like threading. So books aren't always good on
the details and explanations, either.

Thus, I would like to solicit people's opinions on good tutorials and
books (although, I should probably check the FAQ's...but I'd bet
a dollar the Sun tutorial heads the list....).

One helpful resource I found was Roedy Greene's Java section
of his Canadian Mind products website. A very in-depth view
of several of the trickier 'gotchas' with the Java language is there,
as well as a lot of other resources. Google for him/Canadian Mind
Products for details and (current) addy.

TIA,
Tarkin

 
Reply With Quote
 
garethconner@mac.com
Guest
Posts: n/a
 
      01-19-2007

> Thus, I would like to solicit people's opinions on good tutorials and
> books


My favorite Java book is still the O'Reilly 'Learning Java' by Niemeyr
& Knudsen. It covers a good mix of the language and the most common
libraries. In a particular, the coverage of threads, sockets, and
Swing are quite good.

-Gareth

 
Reply With Quote
 
Steve W. Jackson
Guest
Posts: n/a
 
      01-19-2007
In article <(E-Mail Removed). com>,
"Tarkin" <(E-Mail Removed)> wrote:

> Not to be critical, but, to paraphrase you:
> Java's tutorial...not a good source of knowledge on the language
> itself.
>
> and then:
> A Java language tutorial...is where you would learn more
> about what interfaces are and why, among countless other things.
>
> I don't fully disagree (reading the tutorial felt like eating a
> doughnut for dinner), but what does that say to newcomers (including
> myself), that the people who *wrote* the language cannot create a
> satisfactory tutorial?
>
> TIA,
> Tarkin


I didn't mean to suggest that the people who wrote the Java language
*couldn't* make a good tutorial. I merely expressed my opinion that the
tutorials at Sun are good for learning to do lots of different stuff in
Java, with lots of useful examples (I go back to them frequently when
going into new areas). I just never found them all that good as
tutorials about the language itself.

= Steve =
--
Steve W. Jackson
Montgomery, Alabama
 
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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Going from higher security level interface to lower security interface- HELP!!! - AM Cisco 4 12-28-2004 09:52 PM
PCI interface or USB interface David Wireless Networking 4 09-16-2004 01:01 PM
allow ssh only on outside interface, but telnet on inside interface of router no-one Cisco 0 07-28-2004 04:17 PM
PIX: how to allow 1 host from outside interface to access another host on the inside interface? jonnah Cisco 1 04-21-2004 02:26 PM
static nat between phisical interface and virtual interface on same ethernet Andrea Cisco 0 04-19-2004 09:37 AM



Advertisments