Page 1 of 3 123 LastLast
Results 1 to 20 of 53

Thread: Interface?

  1. #1
    maknib is offline Member
    Join Date
    Nov 2010
    Posts
    90
    Rep Power
    0

    Default Interface?

    Hey guys, i am having a really really hard time trying to grasp the concept of interfaces.

    what i can't understand is Why would i have an interface when in the class that implements the interface still has to type the full method out anyway

    so if

    interface Dog has myMethod();

    and class BigDog implements Dog

    inside BigDog i still have to type

    Public void myMethod(){

    }

    so what is the point of interfaces?

    can anyone try to explain this for my tiny mind to understand?

    i am using the Java tutorials but Really can't understand this and don't want ot move on until i grasp it.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Sorry if my thoughts are jumbled, but I'm post call and sleep deprived, but I think you're looking at it backwards. The key thing in my mind is the consumers of the interface, the classes that use the objects that implement interfaces. This code can use (almost) any class that satisfies the interface contract and doesn't really care what code the interface implementing class has as it will work.

    For instance the code that works behind the scenes when a JButton is pressed doesn't have to know what happens inside of the ActionListener implementing object; it only has to know that the object satisfies the interface contract, and this allows for great separation of code and smart program design.

  3. #3
    maknib is offline Member
    Join Date
    Nov 2010
    Posts
    90
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Sorry if my thoughts are jumbled, but I'm post call and sleep deprived, but I think you're looking at it backwards. The key thing in my mind is the consumers of the interface, the classes that use the objects that implement interfaces. This code can use (almost) any class that satisfies the interface contract and doesn't really care what code the interface implementing class has as it will work.

    For instance the code that works behind the scenes when a JButton is pressed doesn't have to know what happens inside of the ActionListener implementing object; it only has to know that the object satisfies the interface contract, and this allows for great separation of code and smart program design.
    this is what i don't get. why does it need to satisfy this code? is it just to make sure that all classes have the same methods or what?

    i really don't get why i dont just make these methods in the new class instead of implementing a interface.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Because this way the code that gets activated when a button is pressed can be have been written years ago (and the Swing library has been written years ago), and doesn't care nor need to know what code you're adding to the ActionListener as long as your class satisfies that ActionListener interface, and because it does so, all the code works magically together. It is a wonderful concept really.

  5. #5
    maknib is offline Member
    Join Date
    Nov 2010
    Posts
    90
    Rep Power
    0

    Default

    hahaq i don't think i'll ever understand this. it all sounds too complicated to me. maybe i'm looking at it wrong maybe i'm just retarded, who knows. i've never understood one thing anyone has ever said to try to explain it to me. as i see it there is no code in the niterface just a mthod being declared. it doesnt do anything that is all defined in the class as what it is to do right?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by maknib View Post
    hahaq i don't think i'll ever understand this.
    No. if you can get a good explanation, perhaps you'll understand it better. I see that pbrockway is online right now, and I'm hoping that he'll do a better job than me in this department.


    it all sounds too complicated to me. maybe i'm looking at it wrong maybe i'm just retarded, who knows. i've never understood one thing anyone has ever said to try to explain it to me. as i see it there is no code in the niterface just a mthod being declared. it doesnt do anything that is all defined in the class as what it is to do right?
    It is complicated, no doubt, but the more that you're mind wrestles with this, the easier it will become. Just the fact that you are asking this question rather than just blindly using interfaces because you were told to do so is a good thing. It shows that you're trying to think like a programmer and is an indicator of future success.

    Luck.

  7. #7
    maknib is offline Member
    Join Date
    Nov 2010
    Posts
    90
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    No. if you can get a good explanation, perhaps you'll understand it better. I see that pbrockway is online right now, and I'm hoping that he'll do a better job than me in this department.



    It is complicated, no doubt, but the more that you're mind wrestles with this, the easier it will become. Just the fact that you are asking this question rather than just blindly using interfaces because you were told to do so is a good thing. It shows that you're trying to think like a programmer and is an indicator of future success.

    Luck.

    Thanks.. i don't want to use something i don't understand. like when i was doing AS3 i got given a jumping script. i tweeked it i played with it asked questions and wouldn't use it till i knew what it was for.

    im sure there is one keywor out there that will trigger my brain to go Ohhh OK!!

    but until then i won't continue on the tutorials till i know this.. oh and the end of the tutorials has this exercise..
    For each new class that you've created above, create an interface that defines its behavior, then require your class to implement it. Omit one or two methods and try compiling. What does the error look like?
    sure i can do this, easily but WHY?! that's the question.. Why hehe

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    A key concept of programming is encapsulation where you separate out code that has different functionality. Say for instance you are trying to create a very large and complex program, and in doing so you are creating different classes for different portions of the program. Say for one class you know what you want it to do, what methods it should have, but you haven't yet begun creating the methods themselves, and yet you still want to be able to have a simple version of the class around to test against other portions of the program. One way to do this and to guarantee that the simpler test class will work with the other code is by creating an interface that abstracts out the functionality you want without forcing specifics. Then if your simple test class adheres to the interface, you can use it to help you build the larger program and to test other portions of the program. Then later you can build the larger more complete version of this interface to go into your production code. Then later if you want to improve on this code, you can do so, as long as the new code abides by the contract specified within the interface. It makes it much easier to modularize your code.

    .... pbrockway ... where are you?

  9. #9
    maknib is offline Member
    Join Date
    Nov 2010
    Posts
    90
    Rep Power
    0

    Default

    hmmm so..

    im making a game.. i have a Player class but haven't yet made the methods to make him Jump and attack.

    so i make an interfance with attack() and jump();

    then the player implements the interface and i can test if it works with these methods?

    and player 2 can do the same?

    is this what you are saying?

  10. #10
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    Hi,

    does it means that interface is like a list of what Player can do?

  11. #11
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,573
    Rep Power
    12

    Default

    An interface does contain something important: a promise.

    When a class says it implements an interface then it promises that it will have the methods and things that are specified in the interface. Providing you can rely on promises they are as good as the real thing.

    Another example of an interface is Comparator which promises to have a method to compare two things of a given type.

    In the Arrays class there is a method - binarySearch() - that takes as arguments a sorted array and an instance of Comparator. Basically it searchs through the array and finds the index of whatever element you are looking for. Now the dumb way of searching would be to look through the array at each element in turn. But the binarySearch() is cleverer than that and takes advantage of the fact that the array is sorted. It sort of bounces around the array looking to see if the elements it sees are "too big" or "too small" and pretty quickly finds what it's after. If you have ever tried to guess a number based on someone telling you "too big"/"too small" you will have an idea of how binarySearch goes about things.

    Now comes the tricky bit. The writers of the Arrays class have no clue about the class of thing that binarySearch() will search for. It turns out that they don't need to know. The binarySearch() method takes a Comparator as one of the arguments and the Comparator promises to be able to compare elements to decide "too big"/"too small".

    When you call binarySearch() you pass an instance of an actual class. But the writers of the Arrays class can't have overloaded searching methods for every class that is or might be created! Instead they simply require a Comparator argument: because a Comparator is a promise to be able to do the right thing.

  12. #12
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    @pbrockwayI was going to blow myself off a cliff with poor explanations, but came back and saw that you had saved me :P Thanks for that!

    @OP: Interfaces will make a lot more sense when you need them. If you've never needed them, then it can be hard to understand why you would need them. For me it worked that way, and interfaces, while important to large projects, are not necessarily very important to beginners in Java.

    You did mention something about it being a way to test whether methods work. It can be integrated with things like JUnit and other testing frameworks to do something similar, but interfaces are not really designed to do that.

    One way it comes in useful, which pbrockway (probably quite reasonably) skipped, is with truly gigantic projects. I've had some projects where I've simply given up on building the program from the bottom up, and designed EVERYTHING as an interface, written the docs, etc, then implemented those interfaces one by one, relying on the methods in my interfaces for functionality within the implementations of other interfaces. I find those programs tend to have smaller classes than ones I write using just classes, which is probably a good thing. It can take much longer, however.

    I stick to my statement that pbrockway explained Interfaces better, and that if you don't understand it at this point, you may just have to wait for it to click. For now, it's enough to understand that by implementing an interface, you state that you will provide implementations for each of its methods. Also, you should realize that if you do not follow the contract for how those methods should behave, by implementing them against the contract, you accept full responsibility for any program crashes/bugs it causes.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  13. #13
    maknib is offline Member
    Join Date
    Nov 2010
    Posts
    90
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    An interface does contain something important: a promise.

    When a class says it implements an interface then it promises that it will have the methods and things that are specified in the interface. Providing you can rely on promises they are as good as the real thing.

    Another example of an interface is Comparator which promises to have a method to compare two things of a given type.

    In the Arrays class there is a method - binarySearch() - that takes as arguments a sorted array and an instance of Comparator. Basically it searchs through the array and finds the index of whatever element you are looking for. Now the dumb way of searching would be to look through the array at each element in turn. But the binarySearch() is cleverer than that and takes advantage of the fact that the array is sorted. It sort of bounces around the array looking to see if the elements it sees are "too big" or "too small" and pretty quickly finds what it's after. If you have ever tried to guess a number based on someone telling you "too big"/"too small" you will have an idea of how binarySearch goes about things.

    Now comes the tricky bit. The writers of the Arrays class have no clue about the class of thing that binarySearch() will search for. It turns out that they don't need to know. The binarySearch() method takes a Comparator as one of the arguments and the Comparator promises to be able to compare elements to decide "too big"/"too small".

    When you call binarySearch() you pass an instance of an actual class. But the writers of the Arrays class can't have overloaded searching methods for every class that is or might be created! Instead they simply require a Comparator argument: because a Comparator is a promise to be able to do the right thing.
    sooo...in this example what is implementing the Comparator cinterface? the Arrays Class?

    and if so are you saying that the Boss said " BOB! you will make an array class! it must implement the comparator interface." and doing so, all Bob needs to know is that he must use the methods inside the comparator?

    what about this..

    i have a Dog interface and it declaresint legs;

    then i had a Greyhound class. it would implement Dog and extend Animal because the Dog interface Promises that the greyhound will have legs?

  14. #14
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,573
    Rep Power
    12

    Default

    sooo...in this example what is implementing the Comparator cinterface? the Arrays Class?

    No - whoever calls the binarySearch() must implement the Comparator ie provide something that lives up to the promise to be able to compare. The Arrays class does something else: it relies on the promise.

    -----

    Something else I was thinking about that may help:

    "interface" is the place where two things come together. On the face of it a strange word to capture the notion of encapsulation or promise or whatever.

    Not really. A railway carriage can be pulled by all sorts of classes of locomotive. And, likewise, a locomotive (of whatever class) can pull all sorts of classes of cars, coaches and wagons. From the point of view of adaptability the important part is the interface: literally the place where the two vehicles meet and are joined.

    It is quite important (for obvious reasons) that all the different sorts of rolling stock share the same connectors (provide the same interface).

    In the real, physical, world the things that allow for interoperability often occur at the physical interface. (Think of the grove shape in a screwhead and the corresponding shape of the screw driver). Programming languages have taken over the word to express the ways that concrete classes with all sorts of other characteristics can promise to have a narrower range of behaviours that will ensure interoperability with other classes.
    Last edited by pbrockway2; 11-04-2010 at 05:21 AM.

  15. #15
    maknib is offline Member
    Join Date
    Nov 2010
    Posts
    90
    Rep Power
    0

    Default

    ohhh so binarySearch is a method declared inside of the Comparator interface?
    and implementing this is promising to do a search..


    A locomotive implements the interface that Promises that the Locomotive will pull the carts or carriage and whatnot?

    My bladder class implements the Beer interface promising that i will need to takeALeak method

    ????

  16. #16
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,573
    Rep Power
    12

    Default

    Maybe you're over thinking things. binarySearch() is declared within the Arrays class. Implementing Comparator is promising to be able to compare(). But I am just repeating what was said before.

    Perhaps better to write some code! The first place you will run into interfaces is when you use other people's libraries. (Swing and the collections framework are common in Java). At that point notice whether the types you are using are concrete classes or interfaces and wonder why.

  17. #17
    maknib is offline Member
    Join Date
    Nov 2010
    Posts
    90
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    Maybe you're over thinking things. binarySearch() is declared within the Arrays class. Implementing Comparator is promising to be able to compare(). But I am just repeating what was said before.

    Perhaps better to write some code! The first place you will run into interfaces is when you use other people's libraries. (Swing and the collections framework are common in Java). At that point notice whether the types you are using are concrete classes or interfaces and wonder why.
    thanks for the help. Don't think i'll ever understand this, 2 days so far hunderds of examples and none of it makes sense, just seems like a useless thing to me.. i mean like in the tutorials..


    In its most common form, an interface is a group of related methods with empty bodies. A bicycle's behavior, if specified as an interface, might appear as follows:
    Java Code:
    interface Bicycle {
    
           void changeCadence(int newValue);   // wheel revolutions per minute
    
           void changeGear(int newValue);
    
           void speedUp(int increment);
    
           void applyBrakes(int decrement);
    }
    To implement this interface, the name of your class would change (to a particular brand of bicycle, for example, such as ACMEBicycle), and you'd use the implements keyword in the class declaration:
    class
    Java Code:
    ACMEBicycle implements Bicycle {
    
       // remainder of this class implemented as before
    
    }

    My whole Discussion is about.. Why the hell Can't i just go..

    Java Code:
    ACMEBicycle implements Bicycle {
    
         void changeCadence(int newValue){
           // wheel revolutions per minute
         }
    
           void changeGear(int newValue);{
    
          }
    
           void speedUp(int increment);{
           
           }
    
           void applyBrakes(int decrement);{
           
           }
    
    }
    i see no need for the interface. But again i don't think i'll ever understand this so i guess i'll just move on in the tutorials and hope i don't need to use it. Seems like quite a hard thing to introduce on the first page of a beginner tutorial :S

  18. #18
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,573
    Rep Power
    12

    Default

    i guess i'll just move on in the tutorials and hope i don't need to use it. Seems like quite a hard thing to introduce on the first page of a beginner tutorial

    I think moving on is the best thing. And I agree about it being a difficult concept in a beginner tutorial. You have got to use the things to understand what they are. That's why it's important to move on ... without forgetting. I think you mentioned elsewhere programming with ActionScript. If you do any Swing programming you will find yourself of necessity implementing interfaces and that's the time to wonder why.

  19. #19
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,573
    Rep Power
    12

    Default

    Also I don't think Oracle's (generally execellent) Tutorial is at its best in the classes and bicycles section. I don't mean to bog you down in tutorials, but you might find something of interest in Bruce Eckel's Thinking in Java.

  20. #20
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    The first page of the beginner tutorial simply introduces the basic parts of Java. Not necessarily the easy stuff.

    So, my suggestion is to move on. Just remember what you need to do when you implement an interface - eventually why the interface is needed will click. Pay attention to when interfaces are used in the Java libraries (places where interfaces are used a lot include Swing, JAXP (Java's XML), RMI, and the audio libraries).

    I didn't even read about interfaces until I hit about my fourth/fifth month of Java programming, and even then it took me a couple of months to realize why they were needed. Give it time!

    EDIT: Missed pbrockway's posts, but I think this is still relevant.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

Page 1 of 3 123 LastLast

Similar Threads

  1. interface
    By tatya in forum New To Java
    Replies: 1
    Last Post: 06-19-2010, 07:22 AM
  2. Is there a GUI interface for...
    By smith427 in forum New To Java
    Replies: 3
    Last Post: 12-03-2009, 08:17 AM

Posting Permissions

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