Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By jim829

Thread: Can someone further explain interfaces? I cannot understand them

  1. #1
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Can someone further explain interfaces? I cannot understand them

    I looked at many tutorials, especially at Interfaces (The Java™ Tutorials > Learning the Java Language > Interfaces and Inheritance)
    However, I am still unclear as to what an interface does, and what exactly the benefit of it is. What exactly does one gain from using interfaces? Also, I read that you put method signatures into the interface. What are those?

    Sorry to ask such a simple question, but none of the texts online will suffice for me :(

    Thanks for any and all answers

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,728
    Blog Entries
    7
    Rep Power
    21

    Default Re: Can someone further explain interfaces? I cannot understand them

    An interface doesn't do anything; it only specifies what an implementation (a class) should do if it implements that interface; it's a form of a contract: if a class implements an interface you can be sure that it implements the methods defined in the interface. Classes can implement zero or more interfaces.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: Can someone further explain interfaces? I cannot understand them

    Quote Originally Posted by JosAH View Post
    An interface doesn't do anything; it only specifies what an implementation (a class) should do if it implements that interface; it's a form of a contract: if a class implements an interface you can be sure that it implements the methods defined in the interface. Classes can implement zero or more interfaces.

    kind regards,

    Jos
    In addition, if the class is abstract, it is not forced to implement methods from the interface.

  4. #4
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: Can someone further explain interfaces? I cannot understand them

    So then, what exactly is the point of an interface? It is just declaring things like ints and Strings, and agreeing to use them?
    Does an interface do anything beyond just declare types? Also, what is the point of "agreeing" to implement method? What benefit do you derive?
    At that point, what is it useful for?

    Thanks for the help!

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,916
    Rep Power
    5

    Default Re: Can someone further explain interfaces? I cannot understand them

    Here is an example. Let's say you're a zoo keeper have a lion, bear, monkey, and elephant class. You need to feed them but they all eat different food. So you have each owner of the class implement a Dinner interface with a single method called feedMe(). Now you can feed each animal without knowing what the animals are by simply calling their feedMe method. It is up to each owner of the class to properly implement the feedMe method by putting in what their particular animal eats.

    An example based more in reality is the Comparable class. It has a single method called compareTo(T o) for any class of type T. The owner/designer of that class must decide what determines how the class' natural ordering is compared to other instances of the same class. So then, as someone who uses the class, I can now pass a list of that class to a sort method which accepts only objects of type comparable to properly sort the class. My sort method does this by simply calling the compareTo method. And even though I don't know anything about the internals of the class, I can confidently sort the list.

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

  6. #6
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,028
    Rep Power
    4

    Default Re: Can someone further explain interfaces? I cannot understand them

    +1 for Jim's example.

    In addition to Jim, you can see an interface as a contract. I'd like to make another example, just to add to your understanding.

    We all have been young, and your mother must have told you at times to clean your room. You have a "contract" with your mom to clean your room. All members of your family have different ways of cleaning their rooms, different "implementations" so you will. HOW you do it doesn't matter to HER, she only wants it done.

    You can actually translate this into Java, and it would look like this:
    Java Code:
    public interface Cleaner() {
       void cleanRoom();
    }
    
    public class Me extends Human implements Cleaner {
       
       public void cleanRoom() {
          cleanLego();
          cleanClothes();
          cleanBatmanCollection();
       } 
    }
    
    public class MySister extends Human implements Cleaner {
    
       public void cleanRoom() {
          moveClothes();
          fiddleWithMakeup();
          callBoyfriend();
          doNothingForHours();
       }
    }
    Now we have defined what each class (familymember) does to clean their rooms, it's time for mom to get us moving:
    Java Code:
    public class Family {
       List<Cleaner> familyMembersWhoMustCleanTheirRooms  = new ArrayList<Cleaner>();
       public Family() {
            familyMembersWhoMustCleanTheirRooms.add(new Me());
            familyMembersWhoMustCleanTheirRooms.add(new MySister());
       }
       
       public void timeToCleanRooms() {
          foreach( Cleaner c: familyMembersWhoMustCleanTheirRooms) {
             c.cleanRoom();
          }
       }
    }
    Now Mom only needs to call timeToCleanRooms(), and it will be done (she hopes) :)
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013

  7. #7
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: Can someone further explain interfaces? I cannot understand them

    Quote Originally Posted by SurfMan View Post
    +1 for Jim's example.

    In addition to Jim, you can see an interface as a contract. I'd like to make another example, just to add to your understanding.

    We all have been young, and your mother must have told you at times to clean your room. You have a "contract" with your mom to clean your room. All members of your family have different ways of cleaning their rooms, different "implementations" so you will. HOW you do it doesn't matter to HER, she only wants it done.

    You can actually translate this into Java, and it would look like this:
    Java Code:
    public interface Cleaner() {
       void cleanRoom();
    }
    
    public class Me extends Human implements Cleaner {
       
       public void cleanRoom() {
          cleanLego();
          cleanClothes();
          cleanBatmanCollection();
       } 
    }
    
    public class MySister extends Human implements Cleaner {
    
       public void cleanRoom() {
          moveClothes();
          fiddleWithMakeup();
          callBoyfriend();
          doNothingForHours();
       }
    }
    Now we have defined what each class (familymember) does to clean their rooms, it's time for mom to get us moving:
    Java Code:
    public class Family {
       List<Cleaner> familyMembersWhoMustCleanTheirRooms  = new ArrayList<Cleaner>();
       public Family() {
            familyMembersWhoMustCleanTheirRooms.add(new Me());
            familyMembersWhoMustCleanTheirRooms.add(new MySister());
       }
       
       public void timeToCleanRooms() {
          foreach( Cleaner c: familyMembersWhoMustCleanTheirRooms) {
             c.cleanRoom();
          }
       }
    }
    Now Mom only needs to call timeToCleanRooms(), and it will be done (she hopes) :)
    That explanation was amazing.
    Thank you!

Similar Threads

  1. Replies: 5
    Last Post: 03-13-2013, 07:53 AM
  2. Replies: 1
    Last Post: 03-08-2011, 03:52 AM
  3. Can't understand the purpose of interfaces
    By Vortexnl in forum New To Java
    Replies: 3
    Last Post: 02-13-2011, 06:10 PM
  4. Replies: 2
    Last Post: 12-13-2010, 02:34 PM
  5. Don't understand interfaces for inheritance
    By BoomPony in forum New To Java
    Replies: 7
    Last Post: 11-28-2010, 09:23 PM

Posting Permissions

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