Results 1 to 20 of 20

Thread: Interfaces

  1. #1
    computerquip is offline Member
    Join Date
    Sep 2009
    Posts
    1
    Rep Power
    0

    Default Interfaces

    I fail to view the point of Interfaces. Let's say you make a interface describing a car and holds the method int stop(), and int go();.

    Java Code:
    public interface Vehicle
    {
       int stop();
       int go();
    }
    I know the functions are constant, cannot be defined, etc. but why is this needed compared to a class? If someone could possibly show me a better example than java.sun.com did, that would be awesome.

    EDIT: A friend of mine explained that it's similar to the purpose of headers in C++. Though I think I understand, could someone help me out?
    Last edited by computerquip; 09-08-2009 at 06:23 AM.

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    What do you mean by the functions are constant?
    Interfaces specify types. All classes that implement that interface indicate that they are the same type. The type of the interface.

  3. #3
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    843
    Rep Power
    6

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Quote Originally Posted by r035198x View Post
    What do you mean by the functions are constant?
    Interfaces specify types. All classes that implement that interface indicate that they are the same type. The type of the interface.
    It's generally termed a contract. The interface essentially says "I provide this functionality", though it doesn't say exactly how it provides it. Which is the point.

    For a simple example, I have an interface for saving stuff:

    Java Code:
    public interface SaveStuff
    {
        void saveStuff(Stuff stuff) throws SaveStuffException;
    }
    Now, in different environments, I can have classes that implement this. One for saving Stuff to a file. One for saving Stuff to a database. One for saving Stuff by calling a web service elsewhere that saves Stuff. This means I can swap these out and I don't have to change the main part of the code that uses this interface.

    ETA: It's nothing like headers in C++.

  5. #5
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Quote Originally Posted by Tolls View Post
    It's generally termed a contract. The interface essentially says "I provide this functionality", though it doesn't say exactly how it provides it. Which is the point.

    ...
    Not all the time. Marker interfaces do not declare any functionality at all. Just type.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Quote Originally Posted by r035198x View Post
    Not all the time. Marker interfaces do not declare any functionality at all. Just type.
    It's still a contract, though. Serializable says "I can be serialised". The lack of a method doesn't change that.

  7. #7
    phoenix123 is offline Member
    Join Date
    Sep 2009
    Posts
    6
    Rep Power
    0

    Default

    refer examples on java2examples.com

  8. #8
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Quote Originally Posted by Tolls View Post
    It's still a contract, though. Serializable says "I can be serialised". The lack of a method doesn't change that.
    I didn't dispute that it's a contract. It's declaring functionality that I indicated that is not always the case.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Quote Originally Posted by r035198x View Post
    I didn't dispute that it's a contract. It's declaring functionality that I indicated that is not always the case.
    To me it's declaring functionality, specifically ability to be serialised. That that functionality resides outside the class itself isn't terribly important in the grand scheme of things.

  10. #10
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    But remember that I can have my own marker interface that does nothing at all.

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Quote Originally Posted by r035198x View Post
    But remember that I can have my own marker interface that does nothing at all.
    And?
    It presumably means something.

    I'm trying to think of an example where that meaning does not involve being able to do something with the implementer of that marker.

  12. #12
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Sure, you can add it to a collection that contains objects of that interface. All that is beside the point. The point here is that the language spec does not require an interface to always declare functionality and that is what I am pointing out.

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Quote Originally Posted by r035198x View Post
    Sure, you can add it to a collection that contains objects of that interface. All that is beside the point. The point here is that the language spec does not require an interface to always declare functionality and that is what I am pointing out.
    Ah right.
    You see I'm not talking about the language spec, but how you would actually use an interface. They are best thought of as contracts that define (whether via methods or not) what the implementer does.

    That I could say:

    Java Code:
    public interface MyInterface {
    }
    
    public class MyClass implements My interface {
    }
    Is irrelevant if MyInterface does nothing...or means nothing. It may as well not exist then. I'm talking in real life here.

  14. #14
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Let me explain it one last time from another perspective.

    Interfaces do not say that "I provide this functionality" as you mentioned in your first post.
    They are a contract saying that if you implement me (not r035198x but the interface), then you must provide this functionality. .i.e the implementing classes are the ones that provide the functionality not the interfaces.
    Now let's apply the principle to marker interfaces. What does the contract of a marker interface say? It simply says that if you implement me (again not r035198x) then you have absolutely no obligations to do anything whatsoever.

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Quote Originally Posted by r035198x View Post
    Let me explain it one last time from another perspective.

    Interfaces do not say that "I provide this functionality" as you mentioned in your first post.
    They are a contract saying that if you implement me (not r035198x but the interface), then you must provide this functionality. .i.e the implementing classes are the ones that provide the functionality not the interfaces.
    Now let's apply the principle to marker interfaces. What does the contract of a marker interface say? It simply says that if you implement me (again not r035198x) then you have absolutely no obligations to do anything whatsoever.
    Ah I see the confusion now.
    My fault...when I said "The interface essentially says "I provide this functionality"", I meant it's presence in a class says that. If I have a method which accepts an object of type SaveStuff I know that object has the functionality related to something of type SaveStuff...in this case a way of saving Stuff.

    But again, wrt marker interfaces, can you come up with one that doesn't imply "I am able to do something"? I'm genuinely curious.

  16. #16
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    An example of that would be a contradiction. A marker interface is by definition void of methods. The implementing class does not have to provide any functionality at all.

  17. #17
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Edit: In fact having read your post again, the functionality provided to marker interfaces is not provided by the implementing classes. That's why it's better these days to use annotations instead.
    In my reply above I thought you had asked for an example of a marker that specifies that it actually does something.

  18. #18
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    So OP, what all of that boils down to is this:
    Interfaces are provided primarily to enable Object Oriented Design principles and in some ways because java does not support multiple inheritance (thank god).

    Action listeners are an excellent example of where interfaces work really well (action, button, mouse, keyboard, etc... listeners).

  19. #19
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Quote Originally Posted by quad64bit View Post
    So OP, what all of that boils down to is this:
    Interfaces are provided primarily to enable Object Oriented Design principles and in some ways because java does not support multiple inheritance (thank god).

    Action listeners are an excellent example of where interfaces work really well (action, button, mouse, keyboard, etc... listeners).
    Spoilsport...;)

  20. #20
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

Similar Threads

  1. Interfaces
    By jon80 in forum New To Java
    Replies: 2
    Last Post: 05-03-2008, 10:57 PM
  2. interfaces..
    By sireesha in forum New To Java
    Replies: 5
    Last Post: 01-16-2008, 06:52 PM
  3. Interfaces
    By Kavana Krishnappa in forum New To Java
    Replies: 7
    Last Post: 12-11-2007, 05:28 PM
  4. Interfaces
    By imran_khan in forum New To Java
    Replies: 5
    Last Post: 07-30-2007, 09:11 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
  •