• Post Reply Bookmark Topic Watch Topic
  • New Topic

Casting  RSS feed

 
Eric Longhorn
Ranch Hand
Posts: 40
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I have a slight question on object casting.
So I have this right here:

and this:

I would like to verify that bb would look like this after the cast:

Are my assumptions correct, or am I missing something here?
 
Tim Cooke
Marshal
Posts: 4039
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That looks about right to me. Those two things are functionally equivalent.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Make sure you understand that at no point does the object you created with new Beta() become anything other than a Beta.  You need to cast the variable b back to a Beta is because the compiler needs assurance that the object that the b references is actually a Beta instance. The cast is your (the programmer) guarantee to the compiler that this is so.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eric Longhorn wrote:. . .. . . am I missing something here?
I am afraid you are missing something. Look in the Java® Language Specification (=JLS) for conversions. Beware: the JLS is notorious for being difficult to read; if you can't understand it, tell us and we'll try to give you a simpler explanation. You are looking for the following three kinds of conversion: identity, narrowing reference and widening reference.

I presume that Beta is a suptype of Baap, otherwise the compiler will notice and decide the conversion is impossible and decline to compile any of your code. The two are not equivalent. What you have is a runtime type of the object and the declared type of the reference. That will make a difference if your Beta class has overridden methods:-In that case there is a foo(int) method in Baap and the compiler knows there will also be a foo(int) method in Beta. In one case it does something and in the other it does something different, and you get whichever is in the runtime type of the object (may be subtype). So far so good. By casting the reference, you are guaranteeing to the compiler that it will be a Beta instance. You can assign that reference to a Baap or a Beta, remembering that under the rules of inheritance a Beta “IS‑A” Baap. There is actually no need for any casting. Such a cast would be a narrowing conversion. If the runtime type of the object is Baap or a different subtype, the cast will be impossible at runtime and will cause an exception to be thrown. As Junilu said, you cannot change the runtime type of an object however hard you try. I don't like casting reference types if I can possibly avoid it.

Now let's have a look at another version of Beta:-If you declare that as a Baap, you cannot use the foo(double) method, because Baap hasn't got one of them. You can get your hands on the method by casting:-
((Beta)myBaap).foo(123456.789); You need two pairs of () because of the precedence of the dot operator.
Since casts are necessary to get at that method, and I don't like casts, I am suspicious of inheritance which adds more methods in subtypes.

Or you can get the method by declaring the reference as a Beta...but you now cannot assign an ordinary Baap to that reference.

Subclass reference: subtypes only, but all subtype methods accessible.
Superclass reference: supertype or subtypes, but new methods in subtypes only exist if reference cast, with risk of errors.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!