Srinivas Katta wrote:I have executed the below Program and getting ClassCastException at Line Marked as Line 10
My Assumption is Redwood is a Tree and we can downcast Tree to Redwood at Line 10
Dear your assumption is partially true
if tree reference variable is pointing to Redwood object , then only you can Downcast.
At runtime, when JVM tries to downcast object referenced by t1 ( which is pointing to tree object) , it generats ClassCastException because you cannot have Redwood Reference pointing to
a tree object. Why this is not allowed is indeed simple to think. Down the inheritance tree ,, you make you classes specialised , may be by overriding and adding new methods. So if subtype reference points to supertype object , how would you call subtype new specialised methods ??? Tree object does not have any idea , how to behave ???
Thanks !!!