Hi,
I was doing some mocks and I faced a question about generics and collection.
That question make me think and I did a simple main program :
My question is:
why this compiles and works fine ?
The output is
To what I've understood about generics and collection, if I declare a collection with the wildcard "?", I should not be able to do an "unsafe" operation, like add, to the collection.
SCJP6 with score 90%. I am conscious of my ignorance and ready to learn from everyone.
Ok I got it...
if you use the keywork "? extends something" you can't add, while if you use "? super something" you can add anithing that is of class "something" or a superclass of something.
Am I right ?!
SCJP6 with score 90%. I am conscious of my ignorance and ready to learn from everyone.
You are absolutely correct, when we say something like
Then we can safely do the add operations. Because the above statement says, only sub-type of Number
is allowed to be added, anything lower in the hierarchy is not allowed.
Today, i was away from ranch, these days even i am dreaming of java-ranch.
yep, that's what I thought. It will accept a List that has elements of Number (or a superclass of Number) and within the method you can add a Number or any sub-type of Number. Ankit - Snap lol