What's the use of an interface

Discussion in 'Java' started by Hakusa@gmail.com, Jan 18, 2007.

  1. Guest

    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.
     
    , Jan 18, 2007
    #1
    1. Advertising

  2. 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
     
    Manish Pandit, Jan 19, 2007
    #2
    1. Advertising

  3. 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
     
    Brandon McCombs, Jan 19, 2007
    #3
  4. Guest

    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.
     
    , Jan 19, 2007
    #4
  5. jupiter Guest

    <> wrote in message
    news:...
    > 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.
     
    jupiter, Jan 19, 2007
    #5
  6. Lew Guest

    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
     
    Lew, Jan 19, 2007
    #6
  7. In article <>,
    "" <> 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
     
    Steve W. Jackson, Jan 19, 2007
    #7
  8. Tarkin Guest

    Steve W. Jackson wrote:
    > In article <>,
    > "" <> 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
     
    Tarkin, Jan 19, 2007
    #8
  9. Guest


    > 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
     
    , Jan 19, 2007
    #9
  10. In article <>,
    "Tarkin" <> 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
     
    Steve W. Jackson, Jan 19, 2007
    #10
  11. On Fri, 19 Jan 2007 12:37:34 -0800, Tarkin <> wrote:
    [sNip]
    > 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?


    It doesn't say anything because there are many different learning
    styles. I know people who found Sun's books to be excellent, while others
    find them to be totally useless, hence I think it's a matter of taste.

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


    That's true. Some teachers really are lousy.

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


    Roedy's web site is excellent, and I was just thinking of it:

    Roedy Green's Java and Internet Glossary
    http://www.mindprod.com/jgloss/jgloss.html

    --
    Randolf Richardson -
    The Lumber Cartel, local 42 (Canadian branch)
    http://www.lumbercartel.ca/
     
    Randolf Richardson, Jan 21, 2007
    #11
  12. Tarkin Guest

    Randolf Richardson wrote:
    > On Fri, 19 Jan 2007 12:37:34 -0800, Tarkin <> wrote:
    > [sNip]
    > > 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?

    >
    > It doesn't say anything because there are many different learning
    > styles. I know people who found Sun's books to be excellent, while others
    > find them to be totally useless, hence I think it's a matter of taste.
    >
    > > 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.

    >
    > That's true. Some teachers really are lousy.
    >
    > > 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.

    >
    > Roedy's web site is excellent, and I was just thinking of it:
    >
    > Roedy Green's Java and Internet Glossary
    > http://www.mindprod.com/jgloss/jgloss.html
    >
    > --
    > Randolf Richardson -
    > The Lumber Cartel, local 42 (Canadian branch)
    > http://www.lumbercartel.ca/


    Sorry all(Steve), Randolf got right to what I meant to say:
    some teachers are really lousy.

    Does anyone know if the rest of c.l.j.p FAQ resources
    have 'good' teachers. i.e. has enough diversity to
    accommodate a reasonable cross-section of learning/
    teaching styles??

    Additionally, since FAQs become outdated, can anyone
    post their (obviously subjective) latest links/pointers
    to 'good' tutorials/books/websites on Java and or OOP?
    And when I refer to OOP, I mean as it will closely relate
    to the Java language, as I have found my C++ books on
    the subject of OOP to not quite line up with an
    effective or applicable Java implementation.

    TIA,
    Tarkin
     
    Tarkin, Jan 21, 2007
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Wildepiet
    Replies:
    0
    Views:
    1,876
    Wildepiet
    Jun 14, 2004
  2. cyberco
    Replies:
    8
    Views:
    506
    cyberco
    Feb 25, 2006
  3. Xah Lee
    Replies:
    15
    Views:
    606
    Sherm Pendley
    Mar 23, 2007
  4. Xah Lee
    Replies:
    15
    Views:
    640
    Sherm Pendley
    Mar 23, 2007
  5. Replies:
    4
    Views:
    357
    timjowers
    Nov 2, 2007
Loading...

Share This Page