Results 1 to 10 of 10
  1. #1
    minime12358 is offline Member
    Join Date
    Jul 2010
    Posts
    21
    Rep Power
    0

    Default What is the point of Interfaces?

    What is the point of Interfaces? From my understanding, it is basically a list of abstract methods. These methods then can be used by implementing the class. Like so in shorthand:

    public interface whatever
    {
    public void joe();
    }
    public class steve implements whatever
    {
    public void joe()
    {
    body
    }
    public static void main(String args[])
    {
    joe();
    }
    }

    So in other words, you going to end up declaring it either way.

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    An interface is kind of like a template or protocol. It provides a format that anything implementing it must follow. For example, when Mr X comes to work on your code, and he wants to create a new class A based on the system you've defined in interface B, he can just implement A from B and knows what functions he has to add.

    This may help you: Interface (Java) - Wikipedia, the free encyclopedia

  3. #3
    minime12358 is offline Member
    Join Date
    Jul 2010
    Posts
    21
    Rep Power
    0

    Thumbs up

    Ok, so it is more of a template. So there is no real ease in coding, like a method, but more of ease for conformity.

  4. #4
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Correct. You have to override all its methods anyway. It's just a way of making the code fit to what was originally intended.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,953
    Rep Power
    19

    Default

    They mean that you can hide implementation details.

    Take the Connection interface for JDBC (database access stuff of Java). This is an interface that simply defines what all Connection classes need to be able to do (or mostly do anyway). Each db vendor has its own version of this for interacting with their database, but the Java code does not need to know how that works, it just needs to know it can get a connection and create Statements.

    Now, it also means that you can change the database at the backend (say from MySQL to Derby or Oracle) and not have to change the main Java code, assuming your db set up data is in a properties file of some sort anyway.

  6. #6
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    have a look at this
    Java Code:
    public interface Card
    {
    public Image getCardImage();
    }
    
    
    
    
    public class PokerCard implements Card
    {
    public Image getCardImage()
    {
    // code here
    }
    
    
    // another implementation
    public class MonopolyCard implements Card
    {
    public Image getCardImage()
    {
    // code here 
    }
    See you got one interface but you can implement it any way you want
    Last edited by al_Marshy_1981; 08-27-2010 at 05:04 AM. Reason: code tags

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Please use code tags when posting code segments here in the forum. Unformated codes are really hard to read. You can find more details about tagging in our forum in FAQ. Links are available in home page as well as in my signature.

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Interfaces are really useful in a case such that you don't want to inherit classes from unrelated classes, to invoke the required functionality.

    For an example assume that you've a class name vehicle with a method fuleType(). It's ridiculous that food class inherit that class just because it has fuelType() method, which is no relevant at all. If you define it in an interface then you can implement in accordingly.

  9. #9
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    The real power of interfaces comes into play when discussing terms such as modularity and encapsulation. The basic premise is, if class A needs class B to do something, class A doesn't have to know how class B performs the operation, just that it does it. Also, consider the following example:
    Java Code:
    public void doSomething(Vector v) {
      //code
    }
    Later on, you decide to use ArrayLists instead of Vectors, and now you have a whole bunch of code to correct. How to avoid it? Interfaces:
    Java Code:
    public void doSomething(List l) {
      //code
    }
    List is an interface, and now this method will work with any class that implements the List interface, making design changes much easier.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Interfaces
    By justin1980 in forum New To Java
    Replies: 9
    Last Post: 02-10-2010, 12:04 PM
  2. Interfaces
    By computerquip in forum New To Java
    Replies: 19
    Last Post: 09-08-2009, 04:58 PM
  3. Interfaces
    By jon80 in forum New To Java
    Replies: 2
    Last Post: 05-03-2008, 09:57 PM
  4. Interfaces
    By imran_khan in forum New To Java
    Replies: 5
    Last Post: 07-30-2007, 08: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
  •