But I got casting exception why, is it valid casting or not ?
Please explain the reaon for this also
Joined: Oct 21, 2008
No, you misunderstood something. I advice you to reread the subject.
Just think about it logically:
Class B extends A. It means that B has everything which A has plus it can have some other methods and fields. So whenever you need an A instance you can use a B instance since it contains everything what is needed for the A instance, but you can't use an A instance as a B instance.
An A type reference can hold an A instance and a B instance. If the reference contains a B instance then you can cast your reference to B. In your example the reference contains an A instance(not a B) so you can't cast it to B.