The output of the above code is "B". My understanding of generics is that the object returned by lis.get(1) is cast to A by the JVM. So why am I not seeing "A" output.
EDIT by mw: Fixed code tags. [ February 24, 2007: Message edited by: marc weber ]
Originally posted by victor kamat: ...My understanding of generics is that the object returned by lis.get(1) is cast to A by the JVM. So why am I not seeing "A" output...
The reference is cast to type A, but the true runtime type of the object remains B. Does that make sense?
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org
victor kamat
Ranch Hand
Joined: Jan 10, 2007
Posts: 247
posted
0
Thanks
Benjamin Samuel
Ranch Hand
Joined: Dec 15, 2006
Posts: 33
posted
0
Here, the subclass B is overriding the toString() method defined in the class A. Thus according to the overriding rules, the invocation of toString() method on the B object(with A's reference) will invoke the B's method. If you had removed the toString() method from B then you would have got 'A' as the output.