Sure, but you'll have to import the class from the other package first, using "import". Then you need to use the package name to be specific as to which class you're referring to, i.e. (package.Classname) myClass
Then it has to be possible to cast one into the other. You can't just cast anything into anything.
Tim Lim wrote:
Even if they cant be cast, is there a way to cast them?
By definition, no.
I think it's true that you can only cast when one is a subtype of the other. (Not sure if it's different for primitives though.) It works implicitly from subclass to superclass, but needs to be explicit from super to sub.
I think if you have 2 unrelated classes you need to write your own method to return a new instance of the required type.