• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

casting a reference

 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


line 1 gives an error , because at compile time methods are associated with reference so compiler will search m() in to Parent class & ....

but in line 2 , we are casting a reference in to another reference ...
whats the funda of casting reference :
1] sub class reference are implicitly casted into super class :
super = sub ; // super & sub are reference of Parent & Child respectively .

2] when we cast super class reference into sub class .

3] can we cast any reference to other reference ( no relation b/w class )


thanks a lot .
[ January 14, 2005: Message edited by: rathi ji ]
 
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess hypothetically, a smart compiler may know that two types aren't related at all, and give you a compile-time error:

Although technically, you could have done:

... since at this point, it could have been possible that s was an Integer, i.e. Integer is related to Object. Anyways, I haven't really spent a whole lot of time casting completely unrelated objects, so this is just a bunch of rambling. There is the wonderful ClassCastException for those times when the compiler allows it, but it's bad at runtime.
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks .
But why line 2 is not giving any exception ...
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because the object that p refers to is an instance of Child. You would get the ClassCastException if it was some instance of a different subclass of Parent.
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So , from all this I got this points :

1] A sub class reference can be implicitly casted in super class reference .

2] A super class reference can be explicitly casted in sub class reference & it will not give any compiler time error but will give run time exception ( ClassCastException )

3] When we try to cast a reference to other that are not related ( child-parent ) then it will give compile time error .

note :
sub class reference refering to sub class object .
super class reference refering to super class object .

if super class reference is refering to sub class object then rule 2 will change and it will not give any exception ...

please .... expert comments required ...

thanks a lot.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Object conversion can be performed without explicit cast if it's an upcast (the object is being converted to a parent type or interface). This is because the subclass is guaranteed to contain at least the elements of the parent or interface without reducing access to members or throwing additional checked exceptions. However, explicit casting is required for the compiler to downcast, because a parent object does not necessarily contain the elements of a subclass.

Note that it's the object reference that's being cast or converted -- not the object itself. The true object type is set at instantiation via the new keyword. The type of a reference variable is known at compile time, but the object's true type cannot be known until runtime.

Upcast conversion is accomplished either by direct assignment or through method call (to fit the argument's type). If being converted to an interface, the original object must implement the interface to which it's being converted. (Thus, an Array can be converted to a Cloneable or Serializable interface, because Array implements these interfaces. An Array can also be converted to another Array of convertible object references.) Explicit casting for upcasts is legal, but unnecessary since conversion will be performed implicitly.

Explicit downcasting is accomplished by preceding the object being cast with the desired type in parentheses. In performing a downcast, the object's true runtime type must be consistent with the destination type -- i.e., it must have been previously upcast (e.g., to Object for storage in a Collection). If a cast is of an incorrect type, then the file will compile, but will throw a ClassCastException at runtime.

[ January 14, 2005: Message edited by: marc weber ]
 
If you're gonna buy things, buy this thing and I get a fat kickback:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic