aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generics Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Generics " Watch "Generics " New topic
Author

Generics

victor kamat
Ranch Hand

Joined: Jan 10, 2007
Posts: 247


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 ]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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
Thanks
Benjamin Samuel
Ranch Hand

Joined: Dec 15, 2006
Posts: 33
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generics
 
Similar Threads
Generics
generics...
generics
Generics
Generics