Results 1 to 8 of 8
  1. #1
    sth
    sth is offline Member
    Join Date
    May 2014
    Posts
    3
    Rep Power
    0

    Lightbulb Interfaces in the Java API

    I understand that interface methods are abstract. I don't understand what the methods in the API do if the method bodies are empty. For example, say there are two interfaces, both with one method with no parameters. What would make these two interfaces different from each other. In the API, the AudioClip interface has the methods play(), stop(), and loop(). If abstract methods have no method bodies, and these methods take no parameters, what makes them different from each other. I know that this isn't a vital question for the Java language, but I'm curious. Thanks for helping

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default Re: Interfaces in the Java API

    When a class implements an interface it needs to fill in the code in the methods.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    sth
    sth is offline Member
    Join Date
    May 2014
    Posts
    3
    Rep Power
    0

    Default Re: Interfaces in the Java API

    So then what is the difference between a the Runnable interface and an interface with one empty method with no parameters?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default Re: Interfaces in the Java API

    Post some code to show what you are asking.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    sth
    sth is offline Member
    Join Date
    May 2014
    Posts
    3
    Rep Power
    0

    Default Re: Interfaces in the Java API

    What is the difference between this-

    interface Runnable
    {
    void run();
    }

    interface audioplayer
    {
    void play();
    }

    If the same code is put into the brackets later on.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default Re: Interfaces in the Java API

    The method names are different. That is what the compiler expects to find defined in the class.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Interfaces in the Java API

    Hi,

    as Norm said: The names are different. That is important for the developer. Technically you could use Runnable instead of audioplayer, but then people who read your code are confused. "Runnable" is not an audio player in their eyes. And "run" is playing the audio? Hard to read.

    And the second point is also important: There are 2 distinct interfaces. So you can have instances that implement Runnable and instances that implement audioplayer.
    If you remove audioplayer and use Runnable there, then you might run into trouble, because the compiler and run system cannot distinct between these instances. An instance that was audioplayer in the past can now be used as Runnable which is simply not intended by the developer. (So you loose a little type safety).

    Konrad

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,021
    Rep Power
    6

    Default Re: Interfaces in the Java API

    Quote Originally Posted by sth View Post
    For example, say there are two interfaces, both with one method with no parameters. What would make these two interfaces different from each other. In the API, the AudioClip interface has the methods play(), stop(), and loop(). If abstract methods have no method bodies, and these methods take no parameters, what makes them different from each other. I know that this isn't a vital question for the Java language, but I'm curious. Thanks for helping
    The difference is what you put in the methods. Even though the methods don't take arguments they do have access to the the instance fields and methods of their implemented class and of other inner classes. It is up to you to put the proper logic in the methods. The logic depends on what the designer of the interface had in mind.

    Also, imagine that your audio player interface used run() instead of play(). Then what happens if you decide you need to implement both interfaces. You can do it and you will not get a syntax error. But you will only have one method to implement. Probably not what you want since the interface contracts (yours by your design or the API's by the JDK architects) are radically different.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. All Classes & Interfaces in Java
    By adurga in forum New To Java
    Replies: 0
    Last Post: 11-30-2012, 01:11 PM
  2. Vlc player interfaces in java
    By sankar1227 in forum Networking
    Replies: 1
    Last Post: 08-06-2011, 09:39 PM
  3. About Interfaces in java
    By vaibhavspawar in forum New To Java
    Replies: 2
    Last Post: 05-27-2011, 09:16 AM
  4. User Interfaces and Java... any component/framework?
    By Darkness in forum New To Java
    Replies: 4
    Last Post: 11-25-2010, 05:28 PM
  5. [SOLVED] Interfaces in Java
    By MuslimCoder in forum New To Java
    Replies: 3
    Last Post: 03-08-2009, 02:12 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •