Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to inherit parent's inner class in this code?

Tags:

java

generics

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?

like image 755
overexchange Avatar asked Aug 31 '15 08:08

overexchange


People also ask

Can we inherit inner class?

A inner class declared in the same outer class (or in its descendant) can inherit another inner class.

How do you define a class that inherits from a parent 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.

Are inner classes inherited Python?

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.

How do you inherit a nested class in C++?

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 .


1 Answers

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).

like image 132
Hoopje Avatar answered Oct 09 '22 23:10

Hoopje