• 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

Casting back a Sub-type to the Super-type

 
Ranch Hand
Posts: 30
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Ranchers!
Check the code below

BUT,
* why do the code prints 'Barking' instead of 'Generic animal sound'?
* Is that casting not considered(or rejected silently) by the JVM?
* Is casting for Ref types / object types?

I couldn't accept the output as it is without any explanation.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

S.R Paul wrote:* why do the code prints 'Barking' instead of 'Generic animal sound'?


Because a refers to a Dog object, and you've overridden the makeSound() method in class Dog. It doesn't matter that the type of the variable a is Animal instead of Dog - in fact, the whole point of polymorphism is that it works this way.

S.R Paul wrote:* Is that casting not considered(or rejected silently) by the JVM?


The cast from Dog to Animal is unnecessary, because a Dog is already an Animal. But the rules of the language allow the cast. It doesn't do anything.

S.R Paul wrote:* Is casting for Ref types / object types?


Casting works on reference types as well as on primitive types. When you cast primitive types, in some cases Java converts values (for example if you cast an int to a float, Java will convert the integer value to the corresponding floating-point value). When you cast reference types, no conversion is done.

What casting a reference type means is that you just tell the compiler "I have some object here, and I want you to treat it as if it of type X". Note that a check will still be done, but at runtime instead of at compile-time. If at runtime it turns out that the object is not really of type X, you'll get a ClassCastException.

Note that casting is in principle unsafe - you're deliberately telling the compiler to not do type checking. You should only use casts when there's really no other way to write the code without a cast.
 
S.R Paul
Ranch Hand
Posts: 30
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jesper

"I have some object here, and I want you to treat it as if it of type X"


So, Casting is a methodology to say to change the way/type how the object should be treated and not as a object conversion technique. (except Primitives, they are really widened /broadened)
Is that correct?
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you are right. Casting tells the compiler, “I am confident this is a Foo, please believe me,” and it is possible to be mistaken in that confidence. Casting a reference type cannot change its actual type.
Yes, primitive casting can alter the value of the variable. I think it is unfortunate that the same word “cast” is used for both.
 
S.R Paul
Ranch Hand
Posts: 30
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:I think it is unfortunate that the same word “cast” is used for both.


Ya, that's the reason for my misconception. Anyway now cleared.
Thank you guys
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well done . . . and “you’re welcome”.
 
30 seconds to difuse a loaf of bread ... here, use this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic