0

Can anyone tell me why i get an error saying

Implicit super constructor Node() is undefined. Must explicitly invoke another constructor

I am aware in some cases eclipse gives this error when there is a mix of Java compiler version with the code but this is not the case for me. Here is my code, and the error is in the Queue2 class at the Queue2 constructor.

import java.util.NoSuchElementException;


public class Queue2<T, Item> extends Node<T> {

    private Node<T> head;
    private Node<T> tail;

    // good practice to initialize all variables!
    public Queue2() {
        head = null;
        tail = null;
    }
    public void enqueue(T newData) {
        // make a new node housing newData
        Node<T> newNode = new Node<T>(newData);
        // point _head to newNode if queue is empty
        if (this.isEmpty()) {
            _head = newNode;
        }
        // otherwise, set the current tail’s next
        // pointer to the newNode
        else {
            _tail.setNext(newNode);
        }
        // and make _tail point to the newNode
        _tail = newNode;



    }

    // in class Queue<Type> …
    public Type dequeue() {

        if (this.isEmpty()) {
            return null;
        }
        // get _head’s data
        Type returnData = _head.getData();

        // let _head point to its next node
        _head = _head.getNext();

        // set _tail to null if we’ve dequeued the
        // last node
        if (_head == null){
            _tail = null;
        }
        return returnData;
        public boolean isEmpty() {
            // our Queue is empty if _head
            // is pointing to null!
            return _head == null;
        }

    }  

Here is the super class...and i realize getters and setters arent complete, but i believe that is irrelevant to my error? :S

public class Node<Type> {
    private Type _data;
    private Node<Type> _nextNode;

    public Node(Type newData) {
        _data = newData;
        _nextNode = null;
    }

    public void setNext(Node<T> newNextNode){ 

    }

    public Node<Type> getNext() {

    }

    public Type getData() {

    }

    public void setData(Node<T> newData){

    }
}

btw, this is just some code to do some queue practice! Thanks in advance everyone!!!

choloboy7
  • 287
  • 2
  • 7
  • 19

4 Answers4

2

I suspect the only constructor for Node<T> is like this:

public Node<T>(T value)

That makes complete sense - a node should have a value. Then your Queue2 constructor fails because this:

public Queue2() {
    head = null;
    tail = null;
}

is implicitly:

public Queue2() {
    super(); // This is what's failing.
    head = null;
    tail = null;
}

What makes far less sense is for Queue2 to extend Node in the first place. Just use composition instead of inheritance. Why would you ever want to treat a queue as a node? What's the node value of a queue? What's the previous node? What's the next node?

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
0

Because You haven't default non-private constructor in Node class. You have to define default constructor in Node class or call another constructor of Node class from your Queue class

Or avoid this crazy solution cause Queue probably will keep Node objects, not derive from Node class

Arsen Alexanyan
  • 3,061
  • 5
  • 25
  • 45
0

The problem is that you have Queue2<T, Item> extends Node<T>, that you don't have a no-arg constructor for Node<T>, and that the constructor for Queue2<T, item> doesn't indicate which Node<T> constructor is supposed to be called.

I think you actually don't want Queue2<T, Item> to be a subclass of Node<T> (you have a has-a relationship, not an is-a relationship), so change this:

public class Queue2<T, Item> extends Node<T> {

to this:

public class Queue2<T, Item> {
ruakh
  • 175,680
  • 26
  • 273
  • 307
0

When you create a class and do not define a constructor, Java define an implicit one for you, with no parameters (and that does nothing).

When this happen but that you inherit from another class (like Queue2 that inherit from Node), this implicit constructor will also call the parent class constructor, so an equivalent of :

public Queue2() {
  super();
}

The error that you see is linked to the fact that the parent class has no default (no-parameters) constructor, so this "implicit" code cannot work.

To solve this, define a constructor yourself, passing any parameter required by the Node constructor. See the official doc on this.

Martin
  • 7,634
  • 1
  • 20
  • 23