#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    1
    Rep Power
    0

    What is the point of interfaces?


    I've read all answers to similar questions that I could find, and I still do not understand why are interfaces useful. I know how they work, but I don't understand why they are needed. In every single example of interfaces (I must have read a dozen tutorials by now), the example code would work exactly the same way if I removed implements SomeInterface and just ignored the whole concept of interfaces.

    Is it just a way of enforcing the use of certain methods? If (for the sake of argument) I plan to never work with other programmers or have them reuse my code, can I just ignore interfaces? Because I sure would like to.
  2. #2
  3. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Feb 2004
    Location
    Mobile, Alabama
    Posts
    4,023
    Rep Power
    1285
    Interfaces are useful because they give you a guaranteed contract on a class.

    No matter what type of object it is, if it implements a certain interface, then it is guaranteed to implement the methods listed in the interface.

    For example, very different classes like java.lang.Double and javax.management.ObjectName both implement the Comparable interface, so an array of either of those objects could be sent to this method.

    Code:
    public void sort(Comparable[] comparables) {
          for (int counter=0;counter<comparables.length;counter++)
             for (int counter1=counter+1;counter1<comparables.length;counter1++)
                if (comparables[counter].compareTo(comparables[counter1]) > 0) {
                   Comparable temp = comparables[counter];
                   comparables[counter] = comparables[counter1];
                   comparables[counter1] = temp;
                }
       }
  4. #3
  5. Daniel Schildsky
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Mar 2004
    Location
    KL, Malaysia.
    Posts
    1,555
    Rep Power
    1621

    Uses of interface


    It is not just about sharing codes with other programmers that you need interfaces for defining the API contract, but it also helps to simplify your coding effort in many scenarios.

    Further to the answer provided by bullet, here is another example to give you an idea of how important the usage of interface is:

    Imagine that you are working on an application that needs to send contents to client's printer. As we all know, there are a variety of printers available in the market, consisting of different brands and for each brand, there could be many different models. Each model could have different implementation of firmware and capabilities controlled by sets of parameters specific to the particular printer model for particular brand.

    In such case, would you be able to guess what is the brand and model your client would be using in the environment your application is deployed to? If you could not, without using the concept of interface,
    • how would you implement the codes to send the data contents to the client's printer?
    • Would you find out all the possible printer brands and their respective models and then implementing a huge if-else block to call to those printer firmware API methods for each of them?
    • Then how much code you need to write and how much time you need to implement the codes to send your application's data to each of these printers just in case your client is using either one of them (as you do not know what printer your client is using)?
    • What if after you have published your application, one of the leading printer brand introduces a new model of printer with different API and new capabilities?
    When the programming world turns decent, the real world will turn upside down.

IMN logo majestic logo threadwatch logo seochat tools logo