2

I currently have a project, where I made an error which I can't figure out why it's giving me that error.

My structure:

Class: Variable (commands.lib.Variable);
Class: ImageLoader extends Variable (commands.lib.Variable);

Then I cast a Variable to an ImageLoader:

// There is a variable made with: Variable v = new ImageLoader();
public static Variable(commands.lib.Variable) getVariable(String value){...}
ImageLoader t = (ImageLoader)getVariable(ab[2]);

But this gives me an error:

Exception in thread "main" java.lang.ClassCastException: commands.lib.Variable cannot be cast to commands.variable.ImageLoader

So of course I googled ClassCastException and this post came out of the result: Explanation of "ClassCastException" in Java

Here someone explains:

Animal a = new Dog();
Dog d = (Dog) a; // no problem, the type animal can be casted to a dog, because its a dog
Cat c = (Dog) a; // raises class cast exception, you cant cast a dog to a cat

So for my code it will look like this:

Variable a = new ImageLoader();
ImageLoader b = (ImageLoader) a; // This should work, but will throw a ClassCastException
Array c = (Array) a; // This should throw a error

So I'm a bit clueless. I hope someone will help me, and fix the error.

Greetings,

Robin

Community
  • 1
  • 1
  • 2
    I think that should work fine.. Can you show some code of your getVariable method? – Rohit Jain Oct 25 '12 at 12:34
  • 1
    Are you for sure returning the variable `v` that you created using `Variable v = new ImageLoader();`? Or returning something else? – Rohit Jain Oct 25 '12 at 12:37
  • 1
    Try stepping through the code to see what type of value t is. Or add some console output like `System.out.println(t.getClass().getName()`. That should give you some insight into what is being returned by getVariable(). – Greg Brown Oct 25 '12 at 12:39
  • Ok I found the problem, I didn't put the ImageLoader in the array. Instead I putted in a Variable. – Jaap Carol Anderson III Oct 25 '12 at 12:52

2 Answers2

0

EDIT: the answer does not actually answer the question, but not deleting as the comments below are helpful in understanding of the underlying concept.

I have always been confused with this

but see

This statement

Variable v = new ImageLoader();

You are storing an object of ImageLoader(Subclass) to Variable(SuperClass) this is fine

But this statement

ImageLoader t = (ImageLoader)getVariable(ab[2]);

You are converting Variable(superclass) to ImageLoader(SubClass) This is not allowed in java, hence the error

Mukul Goel
  • 8,387
  • 6
  • 37
  • 77
  • No, this allowed. And according to your code, it will work perfectly. – Rohit Jain Oct 25 '12 at 12:42
  • I hope I have not got confused this time :) – Mukul Goel Oct 25 '12 at 12:43
  • You have. The cast will fail at runtime, if the superclass reference is not actually pointing to the same subclass instance that you are casting to. But, if it is pointing to the same instance that you are casting to, then it would work happily. – Rohit Jain Oct 25 '12 at 12:44
  • 2
    The problem is fixed. It was that not the correct class was putted in the Array. – Jaap Carol Anderson III Oct 25 '12 at 12:53
  • @RohitJain yea you are right, its a a runtime time issue because of violating compilers compile time trust. by the way Jaap has answered his mistake in comment. He was giving a Variable reference. thus the compile time exception.. cool – Mukul Goel Oct 25 '12 at 12:56
  • @MukulGoel. Not compile time exception. It will be runtime exception. Compiler has no way to find, what is there inside the Variable. – Rohit Jain Oct 25 '12 at 13:06
  • @RohitJain , yea thats what I wrote : "a runtime issue of violating compilers compile time trust" , by Compile time trust i mean, that compiler trusts the programmer that the object being explicitly typecast will be of a compatible type. – Mukul Goel Oct 25 '12 at 13:07
  • @MukulGoel. Yeah that's perfectly a fine sentence. I just wrote that for your last statement that said: - `hus the compile time exception.. cool` – Rohit Jain Oct 25 '12 at 13:14
  • @RohitJain.. oh.. missed that..thanks – Mukul Goel Oct 25 '12 at 13:15
0

In java you're allowed to cast a superclass to a subclass. I've done it before. The problem was that the ImageLoader wasn't made. Where I thought it was made.