• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

is a class both subclass and superclass to itself?

 
ravi satti
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when we can say something like
List<? extends Number> myList1 = new ArrayList<Number>();
does it mean 'a class is subclass to itself'?

when we can say something like
List<? super Number> myList2 = new ArrayList<Number>();
does it mean 'a class is superclass to itself'?
 
Xyz Abc
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when we can say something like
List<? extends Number> myList1 = new ArrayList<Number>();
does it mean 'a class is subclass to itself'?


It means that any class which extends Number can be considered.

when we can say something like
List<? super Number> myList2 = new ArrayList<Number>();
does it mean 'a class is superclass to itself'?


No, it doesn't mean a class is a superclass to itself, it means any class which is a superclass of Number, that the list can be of that "type".

Guys please correct me if I'm wrong!
[ December 01, 2007: Message edited by: Xyz Abc ]
 
Kelvin Chenhao Lim
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe the standard Java terminology does not in fact describe a class as a subclass or superclass of itself. For instance, we usually say that Object has no superclass, or that private members are not visible to subclasses.

However, you're right that the Generics wildcard syntax does seem to be inconsistent with this usage. The way I like to think of it is that the keywords "extends" and "super" are being used in a completely different way here, much like how the "final" keyword has very different semantics when applied to methods and instance variables. Thus, when used in the context of Generics wildcards, "extends" and "super" are more like "subset" and "superset" in mathematics, rather than "subclass" and "superclass". (As you probably know, in mathematics a set A is both a subset and superset of itself.)
[ October 20, 2007: Message edited by: Kelvin Lim ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic