• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Object Reference Casting

 
Suyash Gulati
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can anyone tell me the rules like when you can cast one object reference type to another!
i studied from mala gupta and in that book i thing casting is not given clearly or probably i am not getting the book wants to say.
can anyone specify the rules(all) for casting. like when we should cast explicitly n when not or when it will be a compile time error or a RuntimeException.

(I wrote all because every time i attempt a question about casting i get to know something different OR probably i am getting a bit confused)
Thank You
 
Sergej Smoljanov
Ranch Hand
Posts: 467
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when you has relation with class like:

code like

is always safe.
next:


always compile error. you cant assign object of type Parent to reference type Child.
next

you always have no compiler error. Byt while you run you always get ClassCastException
next

always get compiler error because you can`t assign to reference of type Child, reference variable of type Parent
in this case you must cast

in this example you has no compiler issue and when run you code just work.

next you have definition:

for question why you need cast:

and after you cast you to Child
you can invoke method from Child and access Child`s variable.

also you must read this link to understand interface casting issue
when you attempt to cast one class that has no relation to other that has no relation to first you will get compiler error.


but in case of interface unless class marked as final you can cast to another interface regardless of relation between interface and current class (or link of type interface to current not final class).

also you may want cast to invoke defined overloaded method that accept object reference.(by cast argument you pass)
also you must know how type of link (child or parent) affect when you invoke method that overridden in child but in child it not throw checked exception and in base class throw it.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suyash Gulati wrote:can anyone specify the rules(all) for casting. like when we should cast explicitly n when not or when it will be a compile time error or a RuntimeException.

(I wrote all because every time i attempt a question about casting i get to know something different OR probably i am getting a bit confused)

Maybe you could buy another study guide as an additional resource, because it's a very important topic. And/or have a look at Oracle's tutorial.

It's hard to explain all rules of casting in a single post. It would require several pages in a study guide to do so. It's quite a complicated topic and if you search this forum (and the OCPJP one) you will find plenty of topics discussing doubts about casting.

These threads contain all valuable information (with code snippets to illustrate rules and possible pitfalls):
  • Why will this throw Class Cast Exception?
  • Casting
  • Not sure why my answer on overloading was incorrect
  • Some doubts about casting
  • instanceof operator with an interface versus class


  • Hope it helps!
    Kind regards,
    Roel
     
    Suyash Gulati
    Ranch Hand
    Posts: 37
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Sergej Smoljanov wrote:

    always get compiler error because you can`t assign to reference of type Child, reference variable of type Parent


    but why? p refer to an object of type Child. it should compile, no? :/
     
    Sergej Smoljanov
    Ranch Hand
    Posts: 467
    10
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    try it ;)
    this cause compiler error
    because you can`t assign reference variable of type Parent (regardless of Object that actually hold, or regardless if it hold null value) to reference of type Child. In this case you need to cast.
    this rule is up cast from sub class type to super class type no need cast (but you can do it) , but when down cast super class type to sub class type you always need cast,
    and when you down cast and real object is not actual object type to which you cast or you cast down and actual object is not subtype of type (to which you cast) you will get ClassCastExeption. and if you cast to derived class of sub class and real object is not actual derived class type - ClassCastExeption
    ClassCastExeption you wil get always if actual object is not same tape you cast or sub type of type to which you cast.
     
    Sergej Smoljanov
    Ranch Hand
    Posts: 467
    10
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Sergej Smoljanov wrote:try it ;)
    or you cast down and actual object is not subtype of type (to which you cast) you will get ClassCastExeption.

    (may be first i use wrong words i mean behavior of code below)

     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Suyash Gulati wrote:but why? p refer to an object of type Child. it should compile, no? :/

    The compiler only cares about what it knows at compile time, not at runtime. And that makes lots of sense! Now the assignment is nothing more but creating a new object of type Child, but what if p was initialized using a very complex method? So the compiler will prevent you from doing something stupid like assigning a new String or Dog to a Parent reference variable.


    But although your code compiles successfully you can still have an exception at runtime, e.g. when you try to cast a Parent object to a Child reference variable.


    So in the code snippet you are referring to:

    the compiler only knows p is of type Parent. It doesn't know/care about the actual object it's referring to. So when you want to assign it to c, a reference variable of type Child, an explicit cast is required. Because not all Parent objects will be of type Child and you have to tell the compiler you know what you are doing

    If we switch the types of p and c no cast is required, because it happens implicitly. Because all Child objects will be of type Parent. It's like: all dogs are animals, but not all animals are dogs. So if you want to have an animal do dog-things, you have to tell the compiler explicitly it's a dog (by using a cast). If at runtime the object you thought was a dog happens to be a cat (also an animal), you'll get a ClassCastException at runtime.


    Hope it helps!
    Kind regards,
    Roel
     
    Suyash Gulati
    Ranch Hand
    Posts: 37
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    thank you Roel and Sergej.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic