up vote -1 down vote favorite
1
share [fb]

Last week my lecturer was teaching us about interfaces in Java.

However, I failed to understand her explanation that well.

Does anyone have a good description, or explanation of Java interfaces, and reasons to make use of them?

link|improve this question

50% accept rate
4  
Please google first. – arnaud Oct 9 at 20:51
Talk to the lecturer about it. It'll help you to understand it, and help her to be a better teacher simultaneously. – tjb1982 Oct 10 at 3:26
feedback

3 Answers

Interfaces are a concept that is very similar to, but essentially orthogonal to inheritance.

The basic difference is that java classes can only have one parent class; there is only one thing a derived class can inherit from. The problem with this is that sometimes you would want to use things that are too unrelated to each other to inherit from the same parent, but you want to use them for the same things.

Suppose you're modeling building materials; You have Glue for fastening things together, Caulk for sealing things, Shim for separating things.

Thing is, DuctTape can be used for all of these things. There's no way to have this useful material inherit from all three things; it must choose one.

What this suggests is that it should inherit from none of them; It should just be a specialization of Material and then tell the program that it "can do" all of those things.

Interfaces do exactly that; a java interface doesn't specify how any implementation actually does a thing, only that it can do that thing.

link|improve this answer
feedback

As for the why people use it, there are two basic reasons:

  1. You might be writing an interface for which there are multiple implementations. The java.sql.* interfaces, for example, have different implementations that talk to different RDBMSes, and you load the one you want at runtime, so you can write your code to talk to any of them and just change the one line of code that loads the jar file.

  2. You might be making a "contract" between two programming teams. For instance, a co-worker and I were writing a new program as part of Kodak's now defunct digital cinema project. He wrote the front end, and I wrote the back end. But before we started, we defined an interface that would define all the calls that his front end could sent to my back end, and all the callbacks that I'd call when the operations were done or when operations happened on the backend. We could both start coding our respective sides of the interface, and write testing code to test our sides of the interface, without worrying if he'd implemented the same things as I had.

link|improve this answer
feedback

Interfaces in Java define a contract specification.

Meaning, an arbitrary class X might depend on a feature expected to be implemented by interface Y, but doesn't necessarily care about how that feature is implemented, as long as it produces the result or process that X depends on.

So one could create another class Z to implement interface Y and pass it on to X.

This generates flexibility, as one could always choose to supply class X with a different, possibly more efficient, or specialized version of the feature implemented by interface Y.

Depiction:

class Customer {
  String name;
  int age;

  public Customer(String name, int age) {
    this.name = name;
    this.age = age;
  }

  public String getName() { return this.name; };

  public int getAge() { return this.age; }

  public String toString(CustomerFormat format) {
    return format.formatCustomer(this);
  }
}

interface CustomerFormat {
  String formatCustomer(Customer customer);
}

class InlineCustomerFormat implements CustomerFormat {
  public String formatCustomer(Customer customer) {
    return String.format("%s, %d" years old, customer.getName(), customer.getAge());
  }

class SimpleCustomerFormat implements CustomerFormat {
  public String formatCustomer(Customer customer) {
    return String.format("%s-%d", customer.getName(), customer.getAge());
  }
}
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.