<pre><code>public static void main(String[] args) {

    List&lt;? extends Object&gt; mylist = new ArrayList&lt;Object&gt;();

    mylist.add("Java"); // compile error

}
</code></pre>

<p>The above code does not allow you to add elements to the list and wild cards can only be used as a signature in methods, again not for adding but only for accessing.
In this case what purpose does the above fulfil ??</p>

[{'answer_id': 69797, 'author': 'Ren', 'author_id': 11188, 'author_profile': 'https://Stackoverflow.com/users/11188', 'pm_score': 1, 'selected': False, 'text': "<p>With java generics using wildcards, you are allowed the above declaration assuming you are only going to read from it. </p>\n\n<p>You aren't allowed to add/write to it, because all generic types must be stripped at compile time, and at compile time there isn't a way the compiler knows List are only strings, (it could be any object including strings!) </p>\n\n<p>You are however allowed to read from it since they are going to be at least objects.  Mixing different types are not allowed in java collections to keep things clean and understandable, and this helps ensure it.</p>\n"}, {'answer_id': 69815, 'author': 'Henning', 'author_id': 7034, 'author_profile': 'https://Stackoverflow.com/users/7034', 'pm_score': 1, 'selected': False, 'text': '<p>The point of bounded wildcard types is their use in method signatures to increase API flexibility. If, for example, you implement a generic <code>Stack&lt;E&gt;</code>, you could provide a method to push a number of elements to the stack like so: </p>\n\n<pre><code>public void pushAll(Iterable&lt;? extends E&gt; elements) {\n    for(E element : elements){\n       push(e);\n    }\n}\n</code></pre>\n\n<p>Compared to a <code>pushAll(Iterable&lt;E&gt; elements)</code> signature without a wildcard, this has the advantage that it allows collections of subtypes of <code>E</code> to be passed to the method - normally that would not be allowed because an <code>Iterable&lt;String&gt;</code> is, somewhat counterintuitively, not a subclass of <code>Iterable&lt;Object&gt;</code>.</p>\n'}, {'answer_id': 69825, 'author': 'Stu Thompson', 'author_id': 2961, 'author_profile': 'https://Stackoverflow.com/users/2961', 'pm_score': 0, 'selected': False, 'text': '<p>This works:</p>\n\n<pre><code>List&lt;? super Object&gt; mylist = new ArrayList&lt;Object&gt;();\nmylist.add("Java"); // no compile error\n</code></pre>\n\n<p>From O\'Reilly\'s <a href="http://oreilly.com/catalog/9780596527754/" rel="nofollow noreferrer">Java Generics</a>:</p>\n\n<blockquote>\n  <p>The Get and Put Principle: use an extends wildcard when you only get values our of a structure, use a super wildcard when you only put values into a structure, and don\'t use a wildcard you both get and put.</p>\n</blockquote>\n'}, {'answer_id': 69855, 'author': 'Frank Grimm', 'author_id': 903, 'author_profile': 'https://Stackoverflow.com/users/903', 'pm_score': 3, 'selected': False, 'text': '<p>In his book great \'Effective Java\' (Second Edition) Joshua Bloch explains what he calls the producer/consumer principle for using generics. Josh\'s explaination should tell you why your example does not work (compile) ...</p>\n\n<p>Chapter 5 (Generics) is freely available here: <a href="http://java.sun.com/docs/books/effective/generics.pdf" rel="nofollow noreferrer">http://java.sun.com/docs/books/effective/generics.pdf</a></p>\n\n<p>More information about the book (and the author) are available: <a href="http://java.sun.com/docs/books/effective/" rel="nofollow noreferrer">http://java.sun.com/docs/books/effective/</a></p>\n'}, {'answer_id': 69893, 'author': 'Bruno De Fraine', 'author_id': 6918, 'author_profile': 'https://Stackoverflow.com/users/6918', 'pm_score': 0, 'selected': False, 'text': '<p><code>List&lt;? extends Object&gt;</code>, which is the same as <code>List&lt;?&gt;</code>, fulfills the purpose of generalizing all types <code>List&lt;String&gt;</code>, <code>List&lt;Number&gt;</code>, <code>List&lt;Object&gt;</code>, etc. (so all types with a proper type in place of the <code>?</code>). Values of all of these types can be assigned to a variable of type <code>List&lt;?&gt;</code> (which is where it differs from <code>List&lt;Object&gt;</code>!).</p>\n\n<p>In general, you cannot add a string to such a list. However, you can read <code>Object</code> from the list and you can add <code>null</code> to it. You can also calculate the length of the list, etc. These are operations that are guaranteed to work for each of these types.</p>\n\n<p>For a good introduction to wildcards, see the paper <a href="http://bracha.org/wildcards.pdf" rel="nofollow noreferrer">Adding Wildcards to the Java Programming Language</a>. It is an academic paper, but still very accessible.</p>\n'}, {'answer_id': 70393, 'author': 'rmaruszewski', 'author_id': 6856, 'author_profile': 'https://Stackoverflow.com/users/6856', 'pm_score': 4, 'selected': True, 'text': '<p>Let\'s say you have an interface and two classes:</p>\n\n<pre><code>interface IResult {}\nclass AResult implements IResult {}\nclass BResult implements IResult {}\n</code></pre>\n\n<p>Then you have classes that return a list as a result:</p>\n\n<pre><code>interface ITest&lt;T extends IResult&gt; {\n  List&lt;T&gt; getResult();\n}\n\nclass ATest implements ITest&lt;AResult&gt; {\n  // look, overridden!\n  List&lt;AResult&gt; getResult();\n}\n\nclass BTest implements ITest&lt;BResult&gt; {\n  // overridden again!\n  List&lt;BResult&gt; getResult();\n}\n</code></pre>\n\n<p>It\'s a good solution, when you need "covariant returns", but you return collections instead of your own objects. The big plus is that you don\'t have to cast objects when using ATest and BTest independently from the ITest interface. However, when using ITest interface, you cannot add anything to the list that was returned - as you cannot determine, what object types the list really contains! If it would be allowed, you would be able to add BResult to List&lt;AResult&gt; (returned as List&lt;? extends T&gt;), which doesn\'t make any sense.</p>\n\n<p>So you have to remember this: List&lt;? extends X&gt; defines a list that could be easily overridden, but which is read-only.</p>\n'}, {'answer_id': 36215658, 'author': 'stackinfostack', 'author_id': 6088524, 'author_profile': 'https://Stackoverflow.com/users/6088524', 'pm_score': 0, 'selected': False, 'text': '<p><strong>Java Generics : Wild Cards in Collections</strong></p>\n\n<ol>\n<li>extends</li>\n<li>super</li>\n<li>?</li>\n</ol>\n\n<p>Today I am going to explain you how the wild cards are useful. To understand this concept is bit difficult</p>\n\n<p>Now Suppose you have abstract class and in that you have abstract method called paintObject().</p>\n\n<pre><code>Now you want to use different type of collection in every child class.\n</code></pre>\n\n<p>This below is AbstractMain Method.</p>\n\n<p><strong>Here Steps we have taken for this Abstract Main method</strong></p>\n\n<p><strong>1.</strong> We have created abstract class</p>\n\n<p><strong>2.</strong> In Parameter we have define T(you can use any character)\n       --In this case whichever class implement this method it can used any type of class.\n          ex. Class can implement method like \n             public void paintObject(ArrayList object) or public void paintObject(HashSet object)</p>\n\n<p><strong>3.</strong> And We have also used E extends MainColorTO\n         -- In this case E extends MainColorTo\n         -- It\'s clearly means whichever class you want to use that must be sub class of MainColorTo</p>\n\n<p><strong>4.</strong> We have define abstract method called paintObject(T object,E objectTO)\n         --Now here whichever class is implement method that method can use any class on first argument and second parameter that method has to use type of MainColorTO   </p>\n\n<pre><code>public abstract class AbstractMain&lt;T,E extends MainColorTO&gt; {\n      public abstract void paintObject(T Object,E TO);  \n}\n</code></pre>\n\n<p>Now we will extend above abstract class and implement method on below class\n    ex.</p>\n\n<pre><code>public class MainColorTO {  \n     public void paintColor(){\n           System.out.println("Paint Color........");\n     } \n  }\n\npublic class RedTO extends MainColorTO {\n   @Override\n   public void paintColor() {\n   System.out.println("RedTO......");\n }\n}\npublic class WhiteTO extends MainColorTO {\n   @Override\n   public void paintColor() {\n     System.out.println("White TO......");\n   }\n }\n</code></pre>\n\n<p>Now we will take two example.</p>\n\n<p><strong>1.PaintHome.java</strong>    </p>\n\n<pre><code>public class PaintHome extends AbstractMain&lt;ArrayList, RedTO&gt; {\n    @Override\n    public void paintObject(ArrayList arrayList,RedTO red) {\n        System.out.println(arrayList);\n\n    }\n }\n</code></pre>\n\n<p>Now in above PaintHome.java you can check that we have used ArrayList in first argument(As we can take any class) and in second argument we have used RedTO(Which is extending MainColorTO)</p>\n\n<p><strong>2.PaintCar.java</strong></p>\n\n<pre><code>public class PaintCar extends AbstractMain&lt;HashSet, WhiteTO&gt;{\n    @Override\n    public void paintObject(HashSet Object,WhiteTO white) {\n        System.out.println(Object);\n\n    }\n }\n</code></pre>\n\n<p>Now in above PaintCar.java you can check that we have used HashSet in first argument(As We Can take any class) and in second argument we have used WhiteTO(Which is extending MainColorTO)</p>\n\n<p><strong>Ponint to Remember</strong>\n     You can not use super keyword at class level you can only use extends keyword at class level defination</p>\n\n<pre><code>public abstract class AbstractMain&lt;P,E super MainColorTO&gt; {\n\n    public abstract void paintObject(P Object,E TO);\n\n}\n</code></pre>\n\n<p>Above code will give you compiler error.</p>\n'}]
['https://Stackoverflow.com/questions/69702', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/11193/']