Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generics Declration doubt

 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
  • ArrayList<?> and ArrayList<? extends Object>

  • How are these identical ? i understand here that first syntax means anything is possible and second means Object type only or its super class type but not anything then how are they identical ?

  • The wildcard syntax keyword extends is used to mean either "extends" or "implements". So in <? extends Dog> ,Dog cab be a class or an interface.


  • I understand this with class but i don't know how with interface please give me an example.thanks

     
    Anayonkar Shivalkar
    Bartender
    Posts: 1557
    5
    Eclipse IDE Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    saloni jhanwar wrote:i understand here that (1) anything (2) Object or it's super class

    No. The second argument means anything which extends Object - i.e. Object, or its subclass.

    saloni jhanwar wrote:So in <? extends Dog> ,Dog cab be a class or an interface.

    Yes, but it is reference type, right? I mean, when you pass a parameter to a method, you may pass a reference type of Dog, but it also would be referring to a concrete object (i.e. the class name which you used after 'new' keyword). And since that is an actual object (of a concrete class), that class would be implicitly sub-class of Object, right?

    I hope this helps.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic