• Post Reply Bookmark Topic Watch Topic
  • New Topic

Casting  RSS feed

 
Tom Landry
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Working with these 2 simplified objects and attempt to implement a cast.



This results in the following run-time error.

>java Object1
Inside Object2
Exception in thread "main" java.lang.ClassCastException: Object2 cannot be cast to Object1
at Object1.main(Object1.java:21)

I searched for similar examples and found the following: http://way2java.com/casting-operations/object-casting/

The above looks very similar to what is found in the URL.

What is the difference in the above code?
Should the above casting work and if so, what format or syntax should be used?
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my opinion, casting is something to be used only if unavoidable. Upcasts are never needed and downcasts on primitives risk loss of data. Downcasts on reference types risk casting to the wrong type and an Exception.

If you had given the classes “real” names, you would have found it much easier to understand.
Replace Object2 with “Animal” throughout.
Replace Object1 with “Dog” throughout.
Create a Cat class.

Now you can honestly tell the compiler that a Dog is an Animal. I challenge you, however to tell us that an Animal is a Dog. That is what you are saying when you write (Dog)myAnimal
In your case, one of the Objects is an Object2, and if Object1 is a subclass, it cannot be an Object2. It is like casting new Animal() to (Dog)
 
Tom Landry
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:

Now you can honestly tell the compiler that a Dog is an Animal. I challenge you, however to tell us that an Animal is a Dog. That is what you are saying when you write (Dog)myAnimal
In your case, one of the Objects is an Object2, and if Object1 is a subclass, it cannot be an Object2. It is like casting new Animal() to (Dog)


Within the URL that I provided the sample appears to be doing the same.
Granted I did not run that sample to confirm it works but are you saying the sample in the URL will also fail when executed?
 
Tom Landry
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Went back through the sample in the URL and just noticed my mistake and why that code works and mine failed..... i think.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see the link had Flower and Rose. I suspect you managed to swap them so Object1 is the subclass and Object2 is the superclass. I did mention “real” names, didn't I?
 
Tom Landry
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I see the link had Flower and Rose. I suspect you managed to swap them so Object1 is the subclass and Object2 is the superclass. I did mention “real” names, didn't I?


Yes...you did, but it was good thing I didn't originally do that as I would have missed this little learning opportunity. ;-)

Changing the code to the following and it works and makes sense.

Object2 obj2 = new Object2();
Object1 obj1 = new Object1();
obj2 = obj1;
obj1 = (Object1) obj2;
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tom Landry wrote: . . . I would have missed this little learning opportunity. ;-) . . .
Just goes to show you
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!