This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Originally posted by Denise Saulon: This is a sample from the K&B book (p 559), with a little addition:
I get a compiler error (incompatible types) for this line:
for (Integer o : oa)
If I change it to for (Object o : oa), it's working fine. Integer IS-A Object but how come it is not applicable here?
I would imagine that you are getting the compiler error here because if may be possible that you will try to invoke methods using the Integer reference variable (such as o.intValue()) that Object class cannot do. Consider that an array of Objects is returned when you invoke even though you know that it's an array of Integers. The array is only constructed at run time so at compile time the compiler wont know what the actual array type will be. Regards