views:

322

answers:

5

Hi all,

I had an interview days ago and was thrown a question like this.

Q: Reverse a linked list. Following code is given:

public class ReverseList { 
    interface NodeList {
        int getItem();
        NodeList nextNode();
    }
    void reverse(NodeList node) {

    }
    public static void main(String[] args) {

    }
}

I was confused because I did not know an interface object could be used as a method parameter. The interviewer explained a little bit but I am still not sure about this. Could somebody enlighten me?

+3  A: 

The argument needs an object, which class implements an interface (the parameter).

In pseudo Java the code:

void reverse(NodeList node) {
    // your code
}

is equal to:

reverse(x) {
    if(x == null || x instanceof NodeList) {
         // your code
    }else throw new RuntimeException("Some sort of error.");
}

Note; read more on Interfaces here: http://java.sun.com/docs/books/tutorial/java/IandI/interfaceAsType.html

Pindatjuh
<pedant> The *parameter* is an interface type, the *argument* is an object </pedant>
skaffman
And to be REALLY much a pedent. The argument is a reference to an object :}
Martin Tilsted
+1 for the cogent tutorial link.
trashgod
Thanks for the link to Sun tutorial!
zihaoyu
+5  A: 

It's not the interface "object" being passed to the method, still just a regular object. It's just a way of saying "this parameter will accept any object that supports this interface". It's equivalent to accepting some object of a base class type, even if you're passing in a subclass.

quixoto
+13  A: 

This is in fact one of the most common and useful ways to use an interface. The interface defines a contract, and your code can work with any class that implements the interface, without having to know the concrete class - it can even work with classes that didn't exist yet when the code was written.

There are many examples in the Java standard API, especially in the collections framework. For example, Collections.sort() can sort anything that implements the List interface (not just ArrayList or LinkedList, though implementing your own List is uncommon) and whose contents implement the Comparable interface (not just String or the numerical wrapper classes - and having yout own class implement Comparable for that purpose is quite common).

Michael Borgwardt
Thanks Mike! List example is really instructive and easy to understand.
zihaoyu
+2  A: 

This is called programming to interfaces. You don't code to a specific implementation class of node lists but to the interface implemented by all those implementations.

That way your code will still work if someone writes a new and much better implementation of NodeList after you wrote your reverse method and you don't have to adapt your code for each new implementation of NodeList.

ahe
A: 

actually i am interested in the solution of the initial question, what is the implementation of the reverse method ? - i tried but i failed :-)

thanks

sc3_2
I had no luck either. Could somebody on this site help us out?
zihaoyu
I don't believe it's actually possible as given - there is no way to create or change a NodeList provided.
Carl Manaster