Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Why use interfaces? I'm not grasping this concept.

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Why use interfaces? I'm not grasping this concept.

    Ok, I'm relatively new to Java. I'm getting a pretty good grasp of most of the foundations, but interfaces just baffle me. If you have to define all the methods later on anyway, what's the point of using the interface?

    I'm sure eventually I would get this after a lot more study, but I'd really like to generally understand the concept.

    It's explained in various teaching materials that if you implement an interface, all the methods must be defined in the class or you will have a compile error. So the one possibility I'm thinking of is this... Is the purpose just to make sure that another coder doesn't forget to utilize a certain method? I'll use a gaming example because eventually that's what I'd like to get into.

    A character in a game HAS TO have a style of walking, a voice, a certain set of weapons, etc etc... so the interface guarantees that another coder working on that character won't forget to code in certain aspects of the character? Maybe I'm way off. Any insight would be appreciated. Thanks!


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Why use interfaces? I'm not grasping this concept.

    That's the basic idea, though interfaces generally are used to describe how you can interact with a certain object, not necessarily what properties that object has. Sometimes you don't care how the underlying implementation works, just that it does. This is known as abstraction.

    For example, take a Movable interface which contains one method which moves the object to the specified location. From the point of view from a user, it doesn't matter how the actual object gets moved (pushed, pulled, teleported, etc.). The only important feature is that you can move the object that is Movable, and that abstraction is good enough for the user.

    A more concrete example is the event handler dynamics. The Java API has an ActionListener class which specifies that a certain object is capable of receiving and processing ActionEvents. The source of the event (say, clicking a JButton) doesn't care where the event gets passed to. All it needs to know is who to pass event information to, how to pass the information, and what to expect back. This is captured in the ActionPerformed abstract method of the ActionListener interface:

    void ActionPerformed(ActionEvent e);

    The source must pass an ActionEvent object to a destination object via the ActionPerformed method. This method returns nothing.

  3. #3
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Why use interfaces? I'm not grasping this concept.

    A character in a game HAS TO have a style of walking, a voice, a certain set of weapons, etc etc... so the interface guarantees that another coder working on that character won't forget to code in certain aspects of the character?
    I think this is it.

    And it's not just forgetfulness on the part of 2nd-programmer that you want to guard against. You might want to write code that does a die(Character character). It will invoke the walking's stop() method, empty the weapons set, and make the voice utter something dramatic. You can write this code even before 2nd-programmer has implemented the Character interface. That's because the Character definition defines what you expect a character to be able to do and the compiler will enforce this for any programmer who says "import com.coolgame.Character;"

    ---

    This reliance on the compiler to enforce your types into the future and unknown (aka "strong typing") is Java's way of doing it. Other languages - think JavaScript - aren't so compiler reliant. So long as a JS Object instance has a weapons, a voice and walks you could pass it to die().

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Why use interfaces? I'm not grasping this concept.

    Quote Originally Posted by zeropointfield View Post
    A character in a game HAS TO have a style of walking, a voice, a certain set of weapons, etc etc... so the interface guarantees that another coder working on that character won't forget to code in certain aspects of the character?
    The guarantee is not that the character will have a voice, so to speak. The implementing class could just have an empty bodied method, basically ignoring what you considered an important role for a character. Perhaps a "blob" would have a style of walking and weapons, but no voice after all. The blob would still have to include the voice method, even if it is blank. The guarantee is that if some code asks the blob to speak, the blob can effectively ignore the request without causing the game to crash.

Similar Threads

  1. Interfaces
    By TP-Oreilly in forum Java Theory & Questions
    Replies: 14
    Last Post: July 13th, 2018, 12:57 AM
  2. JADE---Sending an Concept including an concept
    By HansDampf in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 25th, 2014, 12:22 AM
  3. Question Regarding Interfaces
    By davidrobin in forum Object Oriented Programming
    Replies: 1
    Last Post: April 18th, 2013, 04:10 PM
  4. Working with Interfaces
    By imamess in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 13th, 2012, 08:52 PM
  5. Interfaces
    By leyland in forum Java Theory & Questions
    Replies: 4
    Last Post: April 5th, 2011, 08:51 PM