0

I'm going over the implementation of positional lists and I am confused on why we are able to pass in an "instance" of an interface to a certain functions inside a class that implements it.

For the interface Position :

    public interface Position <E>{
            public Object getElement ();
    }

And a Node Class that implements this interface

private static class Node<E> implements Position<E> { 
         private E element; // reference to the element stored at this node
         private Node<E> prev; // reference to the previous node in the list
         private Node<E> next; // reference to the subsequent node in the list
         public Node(E e, Node<E> p, Node<E> n) { element = e; prev = p;next = n;}
         //other functions
}

If we were to use the Node Class in a seperate class, I saw functions that seem to pass in an instance of a Postion interface to the function. For example, a function might look like:

private Node<E> validate(Position<E> p) throws IllegalArgumentException {
    if (!(p instanceof Node)) throw new IllegalArgumentException("Invalid p");
    Node<E> node = (Node<E>) p; // safe cast
    if (node.getNext( ) == null) // convention for defunct node
         throw new IllegalArgumentException("p is no longer in the list");
    return node;
}

Another example

public Position<E> before(Position<E> p) throws IllegalArgumentException {  
     Node<E> node = validate(p);
     return position(node.getPrev( ));
 }

I'm not sure what is actually happening when we are passing in an instance of an interface into a function. If we called the function and passed in a Node variable, how is that related specifically to an interface? As stated above, this is an excerpt from my textbook on Positional List and I can't seem to understand how this abstraction works.

EDIT:

In this function, we set the parameter as a node instead of the interface. If a Node implements Position anyways, what is the point of returning a Position from the function?

    /** Returns the given node as a Position (or null, if it is a sentinel). */
    private Position<E> position(Node<E> node) {

       if (node == header || node == trailer)
       return null; // do not expose user to the sentinels
       return node;
 }
Joe
  • 23
  • 7
  • 1
    That implementation in particular is quite dumb - why accept a `Position` when the first thing you do is check for `instanceof Node`. But in general you use interfaces to abstract from concrete implementations. Not sure what your exact question is, though. Do you know how interfaces and classes work? – Marvin Apr 05 '18 at 07:07
  • 1
    It's not an instance of an interface. It's an instance of a class that implements the interface. – Kayaman Apr 05 '18 at 07:11
  • @Kayaman So when the parameter is Position p and the function returns Position, why would we write it using the interface name instead of the name of class that implements the interface? – Joe Apr 05 '18 at 07:16
  • 1
    Because you don't know all the classes that implement `Position`, and you don't really care. To your method they're all the same, but you can treat them as `Position` objects. Of course like Marvin said, that `validate` method is just garbage. Using `instanceof` is a code smell, and in this case a real stink. You say this is from a textbook? Which one? – Kayaman Apr 05 '18 at 07:21
  • @Kayaman It's from Data Structures and Algorithm in Java by Michael Goodrich. So to clarify from what you are saying, from looking at the code added in my recent edit, by setting the return type as Position , the function will return a class object that implements Position interface. So would this return an object of any class that implements a Position interface? – Joe Apr 05 '18 at 07:27
  • I think you should concentrate on the data structures and algorithms when you read that book. It's clearly not a "best practices of Java" book. See the duplicate, there's plenty of explanations on using different kinds of reference variables to point to different kinds of objects. – Kayaman Apr 05 '18 at 07:32
  • @Kayaman Thanks. Reading through that made more sense. For the validate function, are you saying that it is unnecessary because Position will always be an instance of Node? Couldn't other classes also be implementing Position? – Joe Apr 05 '18 at 08:10
  • Of course, which is why the method is stupid. It accepts a `Position` parameter, but throws an exception if it's not `Node`. The parameter should be `Node`. – Kayaman Apr 05 '18 at 08:12
  • @Kayaman Ok. That makes a lot of sense. Thanks for the help! – Joe Apr 05 '18 at 08:28

0 Answers0