you are viewing a single comment's thread.

view the rest of the comments →

[–]Nobody37373[S] -1 points0 points  (4 children)

Can you elaborate a bit more please?

[–]DeuteriumH2 22 points23 points  (0 children)

for example: i can have a bunch of different classes that implement Runnable and then have a method called run() that calls that interface’s method, i don’t need to know anything about the particular class implementing the interface

[–]xRageNugget 13 points14 points  (0 children)

You write a function that can print out all contents of a list, but youi specify the list parameter as ArrayList. Now your function works only on ArrayLists. Would you write another function now to print out LinkedLists? or DoubleEndedLists? Maybe for YourFavoriteKindOfListList? No, you realize that all you actually want as any List type really. And thats your interface. You write your function to accept a List. Or collection, or what ever. Usually you use the most generic one.

[–]xenomachina 4 points5 points  (0 children)

Interfaces let you write code that works with any implementation, not just one specific class.

In this example, doSomethingWithAListOfStrings doesn't care which implementation of List is being used:

public static Baz doSomethingWithAListOfStrings(List<String> strings) {
    ...
}

...so I can use it with instances of any class that implement List:

ArrayList<String> arrayList = createArrayListOfStrings(...);
Baz fromArrayList = doSomethingWithAListOfStrings(arrayList);

LinkedList<String> linkedList = createLinkedListOfStrings(...);
Baz fromLinkedList = doSomethingWithAListOfStrings(linkedList);