tags:

views:

96

answers:

6

There are two classes

foo.bar.FileUploader and barbar.foofoo.FileUploader

Both are identical and extend Uploader

Somewhere in the code the foo.bar.FileUploader is used and are given as a parameter to my function as an Uploader type.

Now I need to cast it back to FileUploader but the barbar.foofoo.FileUploader. And this gives an error:

ERROR - unhandled error java.lang.ClassCastException: foo.bar.FileUploader cannot be cast to barbar.foofoo.FileUploader

And I can't figure out what's going wrong, I suppose it's because the package is different. In that case: Is there a way to still use the class and get the cast done?

+4  A: 

Technically they're not the same class.

You're getting a ClassCastException because the JVM doesn't see them as the same type at all. Perhaps you should have both classes implement the same interface and cast to that? Or alternatively, just have one class in the first place and get rid of the cast?

Noel M
+2  A: 

For Java, the classes are not identical. Consider this

package zoo;
public interface Animal {}

package zoo.london;
public class Lion implements Animal{}

package zoo.newyork;
public class Lion implements Animal{}

Even if the implementation of both Lion classes is identical and they both implement a common interface, you can not cast vice versa like

zoo.london.Lion lion = (zoo.london.Lion) new zoo.newyork.Lion();  // ClassCastException
Andreas_D
Would give ClassCastException in my world
Kennet
This will not work, since the classes ARE different (even if they are written the same). It's clear from the error that the problem is not the way he did the cast, since it says he was trying to cast from one to the other. This is only a solution if you don't know how to call the cast since you're using the short class name, and can't tell how to cast to the specific one, and even then it would only work if foo.bar.FileUploader is a child of barbar.foofoo.FileUploader, not a different class, and if you know for sure barbarUploader is an instance of the child class.
Andrei Fierbinteanu
@Kennet, @Andrei - ... I was editing while you commented. Sure, you're right, didn't read the question carefully enough.
Andreas_D
OK, you corrected it now, so all is well with world again :)
Andrei Fierbinteanu
+1  A: 

The two classes foo.bar.FileUploader and barbar.foofoo.FileUploader are NOYT the same as they are defined in different places with different names. Thus java thinks they have no relationship to each other,

If you want then to be identical then you have to make then the same class ie import across the packages. It might be easiest to start refactoring to make one of them extend the other.

Mark
+1  A: 

Others already said it: The classes are not the same, just because they look like it. Do the classes implement/extend the same interface/abstract class? cast to the interface or abstract class they are both extended of and use that. What you are trying to do is simply not supported by the JVM. The JVM doesn't care if the classes are identical. If they are really 100 percent identical, it's clearly a messed up design, delete one of the classes.

The Java Language Specification describes exactly what you are allowed to cast and what not.

xor_eq
A: 

Get rid of duplicated code. Even if it would work now, because both classes are identical, what would happen if someone edited class A and forgets about class B? At least then you would have a problem, sir :)

Duplicated code is bad, but duplicated classes seem even worse.

edit: you say they are identical, so i guess theyre copies. That would be bad. But since they both extend Upload, you can cast to Upload instead of FileUploader.

atamanroman
You're absolutely right but the class is coming out of a library we made but lost older dependencies needed to work. In further projects we will definitely approach it a different way.
baklap
A: 

Thanks for the help people.

I just solved the problem by importing the the barbar.foofoo.FileUploader. Now it runs fine!

baklap