Below is the parent class 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;
}
}
.....
}
Below is the derived class 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 class LockableNode<T> extends DListNode<T>
in the above code, error:The constructor DblyLinkList<T>.DListNode<T>(T, DblyLinkList<T>.DListNode<T>, DblyLinkList<T>.DListNode<T>) is undefined
occurs at line super(item, p, n)
This error is resolved by saying: class LockableNode<T> extends DblyLinkList<T>.DListNode<T>
How do I understand this error? Why it got resolved?
A inner class declared in the same outer class (or in its descendant) can inherit another inner class.
Inheritance allows us to define a class that inherits all the methods and properties from another class. Parent class is the class being inherited from, also called base class. Child class is the class that inherits from another class, also called derived class.
Inheritance in Inner ClassInheritance is the capability of one class to derive or inherit the properties from some another class. The benefits of inheritance are: It represents real-world relationships well.
A nested class may inherit from private members of its enclosing class. The following example demonstrates this: class A { private: class B { }; B *z; class C : private B { private: B y; // A::B y2; C *x; // A::C *x2; }; }; The nested class A::C inherits from A::B .
You are redeclaring the type variable T
in the inner class. That means that within the inner class, the T
of the outer class is hidden and cannot be referred to anymore.
Since you have a non-static inner class, you can just remove the type variable T
there:
class DListNode { ... }
because it inherits it from the containing class (and probably you mean that the variables are the same, anyway).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With