I have the following interface:
interface IBasicListNode<T> {
/**
* Returns the current element
*/
public T getElement();
/**
* Gets the next ListNode. Returns null if theres no next element
*/
public IBasicListNode<T> getNext();
/**
* Sets the next ListNode
*/
public void setNext(IBasicListNode<T> node);
}
And a class:
class BasicListNode<T> implements IBasicListNode<T> {
protected T _elem;
protected BasicListNode<T> _next;
public T getElement() {
return this._elem;
}
public BasicListNode<T> getNext() {
return this._next;
}
public void setNext(BasicListNode<T> node) {
this._next = node;
}
/**
* Transverse through ListNodes until getNext() returns null
*/
public BasicListNode<T> getLast() {
BasicListNode<T> tmp = this;
while (tmp != null) {
tmp = tmp.getNext();
}
return tmp;
}
}
And I got the folloeing error:
jiewmeng@JM-PC:/labs/Uni/CS1020/Lab/03/prob 2/LinkedList$ javac BasicListNode.java
BasicListNode.java:1: BasicListNode is not abstract and does not override
abstract method setNext(IBasicListNode<T>) in IBasicListNode
class BasicListNode<T> implements IBasicListNode<T> {
^
Why isn’t java detecting that BasicListNode<T> implements IBasicListNode<T>?
The problem here is that you need to implement the method:
Note the type of the parameter
nodeis in factIBasicListNode<T>and not simplyBasicListNode<T>.Take a look at the way
ArrayListis implemented (here). It implements theCollectioninterface. Yet there is noaddAll(ArrayList<T> list)method. Instead it must implement theCollectioninterface and thus it must implement aaddAll(Collection<? extends E> c)method.In you example you want to change your interface to read as follows:
Then implement the method in your
BasicListNodeclass as follows:*Note: Your
_nextvariable must now be of typeIBasicListNode<T>or you must some how check for and cast toBasicListNode.EDIT:
To be clear
ArrayListcould in fact contain a method calledaddAll(ArrayList<T> list)if it wanted to but that is optional. However, it absolutely must contain a method calledaddAll(Collection<? extends E> c)in order to fully implement theCollectioninterface.The take away of the story is that when implementing an interface your class must contain a method signature that is identical to the interface it is implementing. That is it must have the same return type, the same method name and the parameter list must have the same types and order.
EDIT 2:
To use
instanceofwith generics you will need to use the wildcard<?>as follows:This is needed as
<T>is not a type that can be checked against at compile time since you don’t know what type will be used at runtime. The<?>allows you accept aBasicListNodeof any type generic.As to why the
getNext()method works despite having a slightly different return type has to do with the power of generics. There are a lot of special cases when using generics and requires a little more time to understand it all. For more details I would recommend looking up generics and perhaps taking a look at this post here. The accepted answer will only make things a little more confusing so I recommend taking a look at the second answer provided by Cam