So im getting this ClassCastException when trying to cast my Node<T> to my AVLNode<T>. No idea why.
Here is where it throws an exception.
Node<Integer> n1 = new Node<Integer>(3, null);
AVLNode<Integer> n2 = (AVLNode<Integer>) n1;
How the classes look...
public class Node<T extends Comparable<T>> {
public Node<T> right;
public Node<T> left;
public Node<T> parent;
public T data;
public Node(T data, Node<T> parent) {
this.data = data;
this.parent = parent;
} // ...
And the other class in separate file:
public class AVLNode<T extends Comparable<T>> extends Node<T> {
public int height = 1;
public AVLNode(T data, Node<T> parent) {
super(data, parent);
} //...
Error message:
Exception in thread "main" java.lang.ClassCastException: custom.trees.Node cannot be cast to custom.trees.AVLNode
Basically, because a Node is not a AVLNode - you created it a Node, so it's a Node. If you'd created it as a AVLNode you could cast it to a Node, but not the other way round.
Your node is a SuperClass of AVLNode. You have misunderstood how Java casting works,
you cannot make such casts as such. The only situation where you should cast is,
if you have a node reference that points to AVLnode Object, you can say
Node n1=new AVLNode();
AVLNode n2=(AVLNode)n1;
where, since the object type is the same, the reference can be casted.
What you are trying here is to cast a Node (parent class) Object's Reference to a AVLNode (Sub-Class) reference, which simply isn’t possible!
You are down casting Node to AVLNode. Since n1 is an instance of Node it does not contain the extra implementation provided by AVLNode and this is why you get the casting exception to prevent you from executing a method of AVLNode on a Node instance.
Related
I'm writing this in java.
I've got a BinaryTree Class. It has functions that use the Node class.
I've written a subclass of Binary Tree (TraversalTree) and I've got a subclass of the Node class (OrderNode).
How do I get the TraversalTree functions that inherit from BinaryTree to use the subclass OrderNode??
public class BinaryTree {
public Node root; // starting node of the tree
public Node getRoot() {
return root;
}
}
public class Node {
private int value;
private Node left, right, parent;
//constructor
public Node(int value) {
this.value = value; // data value to be stored in node
left = null; // left child
right = null; // right child
parent = null; // parent node
}
}
So here is the subclass OrderNode
public class OrderNode extends Node {
public int preOrder,postOrder, inOrder;
public OrderNode(int value) {
super(value);
}
}
And then in this subclass of BinaryTree, TraversalTree. I want it to have the subclass OrderNode replace all the Node calls in the BinaryTree functions.
public class TraversalTree extends BinaryTree {
}
I've tried to search this a bunch but I'm hitting a wall. Perhaps I haven't been able to find the right search terms. But either way I can't seem to find any info on this.
You can use super to access the super variables and assign sub class object in super class reference variables.
class TraversalTree extends BinaryTree {
public TraversalTree(OtherNode otherNode){
super.node=otherNode;
}
public OtherNode getRoot(){
return (OtherNode)super.getRoot();
}
}
This should replace all node with otherNode.
Node( T itemArg, Node<T> nextArg ) {
this.item = itemArg;
this.next = nextArg;
}
Is this the right way to represent generic constructor
This is correct, as long as the parameter is specified in the class definition.
EDIT: Ensure item is an instance of T, and next is an instance of Node<T>.
public class Node<T> {
T item;
Node<T> next;
//your constructor
}
I am trying to understand the way, how interface works. After doing some research, I found that interfaces are used to specify what a class must do.
I implemented a method(first())in outer class which will return element of Position<E> instance, but the main point where I get confused is, first() uses a method getNext() from Node class which returns Node<E> object, so why I am able to return Position<E> object instead of Node<E> and I can even return Node<E> object from first() method.
private static class Node<E> implements Position<E> {// Inner Class
private E element;
Node<E> previous;
Node<E> next;
Node(E element, Node<E> previous, Node<E> next) {
this.element = element;
this.previous = previous;
this.next = next;
}
#Override
public E getElement() throws IllegalStateException {
if (next == null)
throw new IllegalStateException("Position no longer valid");
return element;
}
private Node<E> getNext() {
return next;
}
}
Outer class method
#Override
public Position<E> first() {
return header.getNext();
}
Since Node<E> implements Position<E>, each instance of Node<E> is also an instance of Position<E> (or, to be exact, an instance of a class that implements Position<E>).
Therefore you can return an instance of Node<E> in a method whose return type is Position<E>.
why I am able to return Position object instead of Node
Because, Node implements Position and that implies Node is a type of Position. So, Position can hold any class instance, who implemented it. In your case, it is Node class, referring to getNext method, which returns Node type object
private Node<E> getNext() {
return next;
}
I've been working on this program for a few days now and I've implemented a few of the primary methods in my BinarySearchTree class such as insert and delete. Insert seemed to be working fine, but once I try to delete I kept getting errors. So after playing around with the code I wanted to test my compareTo methods. I created two new nodes and tried to compare them and I get this error:
Exception in thread "main" java.lang.ClassCastException: TreeNode cannot be cast to java.lang.Integer
at java.lang.Integer.compareTo(Unknown Source)
at TreeNode.compareTo(TreeNode.java:16)
at BinarySearchTree.myComparision(BinarySearchTree.java:177)
at main.main(main.java:14)
Here is my class for creating the nodes:
public class TreeNode<T> implements Comparable
{
protected TreeNode<T> left, right;
protected Object element;
public TreeNode(Object obj)
{
element=obj;
left=null;
right=null;
}
public int compareTo(Object node)
{
return ((Comparable) this.element).compareTo(node);
}
}
Am I doing the compareTo method all wrong? I would like to create trees that can handle integers and strings (seperatly of course)
To be sure that the element indeed is a comparable object, and avoid all the casts, you could do something like this:
public class TreeNode<T extends Comparable<? super T>>
implements Comparable<TreeNode<T>> {
protected TreeNode<T> left, right;
protected T element;
public TreeNode(T obj) {
element = obj;
left = null;
right = null;
}
#Override
public int compareTo(TreeNode<T> node) {
return element.compareTo(node.element);
}
}
For an usage example:
TreeNode<Integer> node1 = new TreeNode<Integer>(2);
TreeNode<Integer> node2 = new TreeNode<Integer>(3);
System.out.println(node1.compareTo(node2));
The above snippet prints -1 on the console.
compareTo method is applied against TreeNode (passed as node parameter), while you compare it with this.element, which is an Object contained in the TreeNode. Simply change to:
return ((Comparable) this.element).compareTo(node.getElement());
assuming you have getElement method.
Try
public <T> int compareTo(Object node)
{
return ((Comparable) this.element).compareTo( ( TreeNode<T> ) node ).element);
}
For a CS class I am writing a linked list implementation of a linked list interface created by my professor. The assignment requires us to use generics for the list. What I have created, I think, is pretty standard.
public class MyLinkedList<T> implements ADTListInterface {
...
private class Node<T> {
Node<T> head;
Node<T> prev;
public Node(int max) {
...
}
public void shift() {
...
Node<T> newNode = new Node<T>(this.max);
newNode.prev = head.prev;
...
}
}
...
}
At compile time following error is generated:
MyLinkedList.java:111: incompatible types
found : MyLinkedList<T>.Node<T>
required: MyLinkedList<T>.Node<T>
newNode.prev = head.prev;
This error has me very confused. Can anyone explain to me what the issue is?
Here is probably the problem:
private class Node<T> {
The <T> is causing extra problems. Because Node is an inner class, it doesn't need to declare its generic type again.
You should declare the Node class like below:
public class MyLinkedList<T> implements ADTListInterface {
...
private class Node {
Node head;
Node prev;
public Node(int max) {
...
}