TypeCasting Doubt
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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".
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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..
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
John
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
-
1 -
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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?
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Object class is the parent and String class is the child..
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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?).
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
with regards,
Shivdhway Pandey
| moose poop looks like football shaped elk poop. About the size of this tiny ad: The new gardening playing cards kickstarter is now live! https://www.kickstarter.com/projects/paulwheaton/garden-cards |








