• Post Reply Bookmark Topic Watch Topic
  • New Topic

Casting back a Sub-type to the Super-type  RSS feed

 
Ranch Hand
Posts: 30
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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: 56600
172
  • Mark post as helpful
  • send pies
  • 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
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done . . . and “you’re welcome”.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!