Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Can I cast same class but of different packages  RSS feed

 
Tim Lim
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to ask if the same class in another package can be cast.
For example, since both classes are the same in term of same names, attributes and methods but in different package.

Like example Class A = (Class A) Class B;

Even if they cant be cast, is there a way to cast them?
 
Luigi Plinge
Ranch Hand
Posts: 441
IntelliJ IDE Scala Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Luigi Plinge
Ranch Hand
Posts: 441
IntelliJ IDE Scala Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

e.g.


I think if you have 2 unrelated classes you need to write your own method to return a new instance of the required type.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12541
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens when you try it?
 
Tim Lim
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I try, i will get a ClassCastException since I have 2 classes with similar attribute, name just that they are of different packages.

Class A is at com.test.class1
Class B is at com.test2.class2

Note that both class are the same just that I put them in 2 different projects.
 
David Byron
Rancher
Posts: 175
Clojure Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Lim wrote:...since I have 2 classes with similar attribute, name just that they are of different packages....

It's helpful to think of the package as part of the fully qualified name of the class rather than merely as a place where you're storing the class.

In that light, com.ok.alpha.YourClass does not have the same name as, and is not identical to, com.ok.beta.YourClass even if they have similar (abbreviated) class names and members.

This fact implies, as others have noted above, that the possibility of casting will depend on whether the two occupy appropriate positions in an inheritance tree. One must be a subclass of the other.
 
Campbell Ritchie
Marshal
Posts: 55677
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Luigi Plinge wrote: . . . Not sure if it's different for primitives though . . .
It is very different for primitives. Have a look in the Java™ Language Specification, and see whether you can understand it
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!