How to inherit parent inner class in this code?

Below is the parent class of DblyLinkList

 package JavaCollections.list; import java.util.Iterator; import java.util.NoSuchElementException; public class DblyLinkList<T> implements Iterable<T>{ class DListNode<T> { private T item; private DListNode<T> prev; private DListNode<T> next; DListNode(T item, DListNode<T> p, DListNode<T> n) { this.item = item; this.prev = p; this.next = n; } } ..... } 

The following is a derived class of LockableList ,

  package JavaCollections.list; import JavaCollections.list.DblyLinkList.DListNode; public class LockableList<T> extends DblyLinkList<T> { class LockableNode<T> extends DblyLinkList<T>.DListNode<T> { /** * lock the node during creation of a node. */ private boolean lock; LockableNode(T item, DblyLinkList<T>.DListNode<T> p, DblyLinkList<T>.DListNode<T> n) { super(item, p, n); // this does not work this.lock = false; } } LockableNode<T> newNode(T item, DListNode<T> prev, DListNode<T> next) { return new LockableNode(item, prev, next); } public LockableList() { this.sentinel = this.newNode(null, this.sentinel, this.sentinel); } ..... } 

If the class LockableNode<T> extends DListNode<T> in the above code, the error : The constructor DblyLinkList<T>.DListNode<T>(T, DblyLinkList<T>.DListNode<T>, DblyLinkList<T>.DListNode<T>) is undefined occurs on the line super(item, p, n)

This error is resolved by saying: class LockableNode<T> extends DblyLinkList<T>.DListNode<T>

How can I understand this error? Why is this allowed?

+6
source share
1 answer

You are updating a variable of type T in the inner class. This means that inside the inner class T outer class is hidden and can no longer be referenced.

Since you have a non-static inner class, you can simply delete a variable of type T there:

 class DListNode { ... } 

because it inherits it from the containing class (and probably you mean the variables are the same).

+5
source

All Articles