Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Why List<? super Object> takes any type of object ?

 
Ranch Hand
Posts: 50
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have wort a class -




In this class has a method is "addToList". This method can take any object and add to the list. My question is about "List<? super Object" this part. I know that "super" keyword allow to accept the super class of the giving type. But in here why the list object is taking any type of object? why I can not use "extend" instead of "super" in "List<? super Object>" ?

Thank you.
 
Marshal
Posts: 76479
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What sort of things do you think match T super Object? Assume that Object means java.lang.Object and you haven't written yourself another class called Object which you have forgotten about.
 
Muztaba Hasanat
Ranch Hand
Posts: 50
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:What sort of things do you think match T super Object? Assume that Object means java.lang.Object and you haven't written yourself another class called Object which you have forgotten about.



No it is the default class of java that all object inherent.

Object and its super class will match with T super Object. I know about PECS rule but do not understand how <? super T> accept super class and also it sub class.
 
Campbell Ritchie
Marshal
Posts: 76479
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
  • 1: What is the superclass of Object?
  • 2: For the purposes of generics, every class is a supertype (and also a subtype) of itself.
  • That means your List will accept anything which is an Object. Actually that isn't true. You cannot add things to a List of unknowns. You can only get things out and be sure they are Objects or instances of its supertype.
     
    author
    Posts: 23928
    142
    jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Muztaba Hasanat wrote:
    Object and its super class will match with T super Object. I know about PECS rule but do not understand how <? super T> accept super class and also it sub class.



    I think that this may be a misunderstanding of the generics bounded wildcard concept itself. I believe that the OP may be thinking that the "extends" keyword when used during sub-classing is the same as when used with a bounded wildcard. In order words, the OP thinks that the List<? super Object> can only be a list that hold objects and super class of objects (the latter doesn't exist)... so, hence, can't be used to hold something like String, etc.

    Is that correct? Is that the understanding?

    Henry

     
    Muztaba Hasanat
    Ranch Hand
    Posts: 50
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Henry Wong wrote:

    Muztaba Hasanat wrote:
    Object and its super class will match with T super Object. I know about PECS rule but do not understand how <? super T> accept super class and also it sub class.



    I think that this may be a misunderstanding of the generics bounded wildcard concept itself. I believe that the OP may be thinking that the "extends" keyword when used during sub-classing is the same as when used with a bounded wildcard. In order words, the OP thinks that the List<? super Object> can only be a list that hold objects and super class of objects (the latter doesn't exist)... so, hence, can't be used to hold something like String, etc.


    Sorry I did not understand. It is a bit complex sentence to me.

    In Collections class the sort method signature is like -

    Could you explain why Comparator use super instead of extend ? Does it support PECS rule ?
     
    Marshal
    Posts: 8431
    606
    Mac OS X VI Editor BSD Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Muztaba Hasanat wrote:Could you explain why Comparator use super instead of extend ?

    If you consider that c could be an object for instance, in this case by writing "? super T" you state, that ? is a superclass of T.

    In slightly different words it sorts list using a comparator c that can compare anything that is of type T or more general, what is above T, superclass of it.

     
    Marshal
    Posts: 27375
    88
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    A Comparator<? super T>, when instantiated as a specific class, is a Comparator<X> for some type X for which T extends X. If you have an object of this type then it can be used to compare two objects of type X, that's what "Comparator<X>" means. So since T is a subtype of X, it follows that the object can also be used to compare two objects of type T.

    Don't make the mistake of believing that a Comparator<? super T> can compare any two objects of supertypes of T. That isn't what it means, much as it may look that way.
     
    He was expelled for perverse baking experiments. This tiny ad is a model student:
    Garden Master Course kickstarter
    https://coderanch.com/t/754577/Garden-Master-kickstarter
    reply
      Bookmark Topic Watch Topic
    • New Topic