File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generics question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generics question" Watch "Generics question" New topic
Author

Generics question

shiva shankar
Ranch Hand

Joined: Jun 10, 2001
Posts: 84
I am trying to understand why I should be use something like <? extends Shape>
For eg if I were to code


Why cant I use just Shape directly

Perhaps I am missing a better example.
Bhavesh Saraswat
Greenhorn

Joined: Jan 20, 2013
Posts: 18

shiva shankar wrote:Why cant I use just Shape directly

Perhaps I am missing a better example.


Wel this code can only accept List<Shape> . It means that you can not use subclass of Shape while calling this method. It can only be List of Shape nothing else.
For this reason generics are diffrent from arrays. To enable this method to receive any subclass of Shape you have to use syntax List<? extends Shape> shape. But there is one constraint here to use this syntax, you can not modify the list in the method. you can only read the list content. If you try to modify then it will give an exception.


OCPJP SE6
shiva shankar
Ranch Hand

Joined: Jun 10, 2001
Posts: 84
Got it. Thanks a lot
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generics question
 
Similar Threads
Generics "is not applicable for the arguments"
Generics doubt
Generic methods and wildcards
Generics
Generics: Shape example does not work as SUN says