• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

TypeCasting Doubt

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

I am newbie to scjp forum. Ready to take my exam. I am having a doubt regarding "type casting of parent and child objects".


Suppose I am having this code snippet




Above code compiles fine .At runtime gives java.lang.ClassCastException.

I am just typecasting parent to child.


But this code works fine


What is the difference between these 2 code snippets...both are typecasting from parent to child.

Thanks & Regards,
Vipul Kumar.
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!

Your first code snippet dont work because you can think of this: Child is a specailisation of Parent. Maybe the Child class has methods that the Parent class does not have, so if you cast Parent to Child this is a "polymorphistic problem".
 
vipul John
Ranch Hand
Posts: 253
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply.

Viktor Pergjoka wrote:Hi!

Your first code snippet dont work because you can think of this: Child is a specailisation of Parent. Maybe the Child class has methods that the Parent class does not have, so if you cast Parent to Child this is a "polymorphistic problem".




Your answer is not accurate. May be you didnot read the total post. i have mentioned first snippet code will compile but will result to java.lang.ClassCastException.

I want the difference between the code snippets..Once again go through the post...

Thanks once again..

 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There isn't anything in the ArrayList, soreturns false straight from the beginning and the while loop with the type cast is never executed. Try adding something to the ArrayList.

John
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your first code snippet is legal totally with compiler, error just occurs in run time.

Second code snippet, you need to supply more information about arraylist object used here.

If it's as declared, it's empty list and cannot go inside while loop, so do not have any exceptions thrown.
 
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

vipul bondugula wrote:
Your answer is not accurate. May be you didnot read the total post. i have mentioned first snippet code will compile but will result to java.lang.ClassCastException.
I want the difference between the code snippets..Once again go through the post...



Do you know what is ClassCastException?
Child is a Parent that means // Child extends Parent.
In line 1, you are not following the rules of OOPS. How can a parent be a child ever?

Your Code 2- Your are using a raw list.(means you are not specifying what type of objects should be stored in this list i.e. you are not using generics).
So on line 2,Casting is required as compiler don't what type of objects you are retrieving from your list.

See Below the Code, casting is not required as we have specified what type of things are there inside in a list.

 
vipul John
Ranch Hand
Posts: 253
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everyone & sorry..

My intention is not about arraylist or something else..

difference between typecasting of both codes....

anyway for your clear clarity of code..I am posting total code....







I am again clearly stating that,

both are typecasting from parent to child. but first one gives runtime exception while second one works fine in typecasting..please check typecasting clearly....

Thanks & Regards,
Vipul Kumar.
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the first case, you're casting a Parent reference to a Child reference. The compiler allows this, because a Parent reference can point to a Child object (e.g. Parent p = new Child()). But in this case it doesn't - it points to a Parent object. A Parent IS-NOT-A Child, and so the cast fails with a ClassCastException.

In the second case you are casting an Object reference to a String reference. Again, the compiler allows it. In this case, though, the reference actually is pointing to a String (because you added Strings to the collection). So the cast works. To turn your second example into the equivalent of the first, try adding a1.add(new Object()); before the loop.

When you are casting, what you are effectively doing is telling the compiler "trust me - I know the reference is of this type, but it's actually of that type". The compiler will allow it as long as it might be correct. But the runtime will only allow it if it is correct.
 
Arjun Srivastava
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

vipul bondugula wrote:anyway for your clear clarity of code..I am posting total code....


First of all ease up mate.

vipul bondugula wrote:
both are typecasting from parent to child. but first one gives runtime exception while second one works fine in typecasting..please check typecasting clearly....
Vipul Kumar.


In first case,casting is wrong.You can't cast Parent to a child.(Parent cannot be cast to Child).

vipul bondugula wrote:


In 2nd Code,You know you are adding strings in a list but compiler doesn't know that you adding strings. So to be on the safeside,Casting is required.And it works well here.

vipul bondugula wrote:both are typecasting from parent to child.


Btw , why do you think that in 2nd code" parent to child casting is there"? Please specify what is parent and what is child?
 
vipul John
Ranch Hand
Posts: 253
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Arjun,

Object class is the parent and String class is the child..
 
Ranch Hand
Posts: 47
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

vipul bondugula wrote:Thanks Arjun,

Object class is the parent and String class is the child..




But you didn't put pure Object instance to the list - there are only String objects, try to add new Object() to your list and you will see ClassCastException at runtime (Object cannot be casted to String). This is real basis - object of parent class cannot be casted to child (how would it be possible? How would you ie. call child method on parent object?).
 
Arjun Srivastava
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tomasz Sochanski wrote:
But you didn't put pure Object instance to the list - there are only String objects, try to add new Object() to your list and you will see ClassCastException at runtime (Object cannot be casted to String). This is real basis - object of parent class cannot be casted to child (how would it be possible? How would you ie. call child method on parent object?).


True!

vipul bondugula wrote:Object class is the parent and String class is the child..


Both Child and Parent are String that's why you are able to do casting there.

 
vipul John
Ranch Hand
Posts: 253
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for every one.. i got it..
 
Ranch Hand
Posts: 67
PHP Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
lease mark if solved problem dear
 
Oh. Hi guys! Look at this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic