I have a Node<T> (inner) class in Graph<T> class:
public class Graph<T> {
private ArrayList<Node<T>> vertices;
public boolean addVertex(Node<T> n) {
this.vertices.add(n);
return true;
}
private class Node<T> {...}
}
When I run this:
Graph<Integer> g = new Graph<Integer>();
Node<Integer> n0 = new Node<>(0);
g.addVertex(n0);
The last line give me error:
The method addVertice(Graph<Integer>.Node<Integer>) in the type Graph<Integer> is not applicable for the arguments (Graph<T>.Node<Integer>)
Why? Thanks in advance?
Your inner class should not override
TSinceTis already used in outerclass. Consider what can happen if it was allowed. Your outer class would have referred toIntegerand Inner class would have referred to another class that too for the same instance.Or you can use
Static Inner classsince Static Generics Types are different than instance generic types.For More Explanation you can Refer to JLS # 4.8. Raw Types