Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within OCPJP
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Programmer Certification (OCPJP)
Covariant
Saibabaa Pragada
Ranch Hand
Posts: 162
posted 13 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi, In this Covariant example, At Line # 12, Are we getting Line#7 return type "Integer" (or) Line#8 return type "int" ("i" is of int type) ?
abstract class AbstractTest { static int i; abstract Object test(); } public class CoVariant extends AbstractTest { Integer test() { return i; } public static void main(String[] args) { System.out.println(new CoVariant().test()); } }
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
posted 13 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
The return type of the method is Integer, so that is what is returned. In this case, i is autoboxed. The main method expects an Integer and that's what it's getting.
Tom Reilly
Rancher
Posts: 618
posted 13 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Try this code. It prints the class of the returned object.
public static void main(String[] args) { Object o = new CoVariant().test(); System.out.println(o.getClass().getName()); }
Don't get me started about those stupid
light bulbs
.
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Inner Classes
abstract and annonymous classes
Use of constructor in abstract class?
K & B 1.5 Chapter 8 self Test Q.12
anonymous class example
More...