• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generic Question

 
Harpreet Singh janda
Ranch Hand
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 182
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Harpreet Singh janda

means anything that is super class of Number or Number class

change
 
Kamil Wojcik
Ranch Hand
Posts: 78
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Anbarasu Aladiyan
Ranch Hand
Posts: 182
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21008
77
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic