aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generic Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generic Question" Watch "Generic Question" New topic
Author

Generic Question

Harpreet Singh janda
Ranch Hand

Joined: Jan 14, 2010
Posts: 317

This Question is from Niko's Mock questions:



This is not legal. Why?

Whether this is legal :



Integer is a sub class of Number. Why we can't put a Integer where we can put a Number?
Anbarasu Aladiyan
Ranch Hand

Joined: Jun 02, 2009
Posts: 182

Hi Harpreet Singh janda

means anything that is super class of Number or Number class

change


A.A.Anbarasu
Kamil Wojcik
Ranch Hand

Joined: May 19, 2008
Posts: 78

List<? super Number> could represent List<Number> or List<Object>, where List<? extends Number> means that the given list contains objects of some unknown type which extends the Number class


SCJP 6.0 Passed | SCWCD Passed | PSM in progress...
Anbarasu Aladiyan
Ranch Hand

Joined: Jun 02, 2009
Posts: 182

Hi Kamil Wojcik,

List<? super Number> could represent List<Number> or List<Object>, where List<? extends Number> means that the given list contains objects of some unknown type which extends the Number class


read it Closely,

that is not
which extends the Number

that is
which is extended by Number
that is super class of Number
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18117
    
  39

Harpreet Singh janda wrote:


This is not legal. Why?

Integer is a sub class of Number. Why we can't put a Integer where we can put a Number?



The compiler is merely doing what you asked for. You stated that the set type is for a wildcard with a lower bound, and then you assigned it a set that don't conform to the bound.

Keep in mind, we are not talking about the elements in the set. We are talking about the type that the set can handle. So, yes, you can add in Integer into a set designed for Numbers, but that doesn't make an integer set and a number set interchangeable.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
Don't get me started about those stupid light bulbs.
 
subject: Generic Question
 
Similar Threads
Concept of Set
How to work with sets?
equals() vs inheritance
TreeSet - Object input and output
Comparing two collections...