• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Can I cast same class but of different packages

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 441
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What happens when you try it?
 
Tim Lim
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 175
Clojure Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic