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".
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...
vipul bondugula wrote:anyway for your clear clarity of code..I am posting total code....
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.
vipul bondugula wrote:
vipul bondugula wrote:both are typecasting from parent to child.
vipul bondugula wrote:Thanks Arjun,
Object class is the parent and String class is the child..
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?).
vipul bondugula wrote:Object class is the parent and String class is the child..
with regards,
Shivdhway Pandey
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
|