"Incompatible types" error though types are same generic type

W

wdh3rd

Hello everyone,

Even though two methods both use the generic return type - E - I'm
getting an error saying that they're incompatible types. Here's the
truncated code:

public class CircularLinkedList<E> extends AbstractList<E> {

private class Node {

private E element;

public E element () {
return this.element;
}
} //end Node

private class CircularLinkedIterator<E> implements
dataStructs.Iterator<E> {

private Node current;

public Node current () {
return this.current;
}

public E get () {
return current.element(); //error is at this line
}
} //end CircularLinkedIterator

}//end CircularLinkedList


The error that I receive reads:

Error: incompatible types
found: E
required: E

I'm at a loss for understanding why the same generic types could
render an incompatible types error. Can anyone shine some Java
wisdom?? Thanks for any help anyone can offer.
 
W

wdh3rd

Sorry, I forgot to mention that the AbstractList<E> that is being
extended by the CircularLinkedList<E> is not the AbstractList<E> from
the Java API, but my own AbstractList<E>.
 
P

Peter Sestoft

Even though two methods both use the generic return type - E - I'm
getting an error saying that they're incompatible types. Here's the
truncated code:

public class CircularLinkedList<E> extends AbstractList<E> {
private class Node {
...
} //end Node

private class CircularLinkedIterator<E> implements dataStructs.Iterator<E> {
...
public E get () {
return current.element(); //error is at this line
}
} //end CircularLinkedIterator

}//end CircularLinkedList

You have two distinct type parameters both called E.

Peter
 
W

wdh3rd

You have two distinct type parameters both called E.

Peter

Thanks. I hadn't thought of that. I made the CircularLinkedIterator
non-generic and everything seems to be kosher now.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top