java


why cant i call method from the class i created with generics? java


I'm currently learning java, and I encountered a problem with generics that I can not solve.
So I have this class:
public class EntityRepository <Entidade extends Entity>{ "code" }
with this method:
public Long create(Entidade ent) {
ent.setId(this.nextId());
this.m1.put(ent.getId(), ent);
return ent.getId();
}
and now i have another method in another class like this:
public static void subMenu(EntityRepository<? extends Entity> repository, String entidade)
which has this code:
Entity item = new Entity();
if(entidade.equals("produto")){
item = new Product();
}
else if(entidade.equals("prateleira")){
item = new Shelf();
}
repository.create(item);
repository.printList(entidade);
so basically when String 'entidade' is equal to strings "produto" or "prateleira" it is supose to call the 'create' method from the first class I mentioned above but with either a 'Product' or a 'Shelf' as a parameter. (which are objects that extend to Entity)
my problem is that is that has this error:
"The method create(capture#1-of ? extends Entity) in the type EntityRepository is not applicable for the arguments (Entity)"
and i cant seem to understand why nor can i find any clues around the web...
can some one help me?
thanks!
First of all, you should stick to naming conventions regarding generic type parameters, which state that they should consist of a single capital letter. So let's change your code accordingly:
public class EntityRepository<E extends Entity> {
// code
}
Now, your problem consists of a misunderstanding of the PECS principle. In short, PECS stands for Producer Extends Consumer Super.
It means that if a parameterized type, i.e. EntityRepository produces a type that matches the type of its parameter, i.e. Entity, you should use the keyword extends. By produces I mean that one or more methods of the parameterized type return a value whose type is the type of the generic parameter. In your example, in the subMenu method, if EntityRepository had a method that returns a descendant of Entity (which is its generic type parameter), then it would be a producer.
On the contrary, if a parameterized type, i.e. EntityRepository consumes a type that matches the type of its parameter, i.e. Entity, you should use the keyword super. By consumes I mean that one or more methods of the parameterized type receive one or more arguments whose type is the type of the generic parameter. In your example, in the subMenu method, if EntityRepository had a method that accepts a descendant (yes, a descendant!) of Entity (which is its generic type parameter), then it would be a consumer.
Now, within the subMenu method, you are invoking the EntityRepository.create method, which receives an Entity as an argument, so, according to the PECS principle, the signature of your subMenu method should be:
public static void subMenu(EntityRepository<? super Entity> repository, String entidade)
which means that, within the scope of the subMenu method, EntityRepository is a consumer of Entity.

Related Links

Java regex for yyyymmddhhmmss
Trying to access MQ 7.5 server through java client but getting error,has created SYSTEM.SSL.SVRCONN
Using Google Api through Android but facing an issue
Query string not preserved in SAML HTTP Redirect binding
Singleton - Spring Bean why not thread safe
How to sort generic list by checking these 3 conditions that is name age and rollno
Only use Raspberry Pi HDMI port by my program
java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0) problems
Wrong IndexOutOfBoundsError [closed]
How to use Netflix ObservableResult and rxJava within Asynchronous mode
if statement does not seem to ever evaluate false
Converting large sql queries into json
Maven failure to find codehaus artifact org.codehaus:codehaus-parent
Unhandled exception Class.forname(“com.google.cloud.sql.jdbc.Driver”) in Android Studio
Any helper methods for stop/start on the Google Cloud Datastore?
How can I declare a circle as a variable? [closed]

Categories

HOME
hive
atom-editor
comparison
plot
react-virtualized
objectgears
rsync
filtering
google-oauth
dxl
azure-media-services
esper
fancybox-3
collectd
remote-access
commonmark
tostring
serilog
database-replication
excel-vba-mac
zapier
opencover
cloudhub
one-to-many
cas
pingfederate
fluentvalidation
procdump
librato
socialengine
media-queries
semantic-versioning
android-nestedscrollview
ghost4j
environment-modules
picasso
io-redirection
hot-module-replacement
streamreader
leading-zero
nomethoderror
vao
sencha-touch-2.3
atomicity
prolog-setof
domain-model
tizen-native-app
git-diff
sqlclient
zendesk-app
flutterwave
jspdf-autotable
crosswalk-runtime
smart-table
httplistener
sdhc
endeca-workbench
deis
myo
processmodel
ami
angular-local-storage
truevault
sorl-thumbnail
codeigniter-url
asp.net-dynamic-data
xcode-6.2
bitcoinj
flask-cors
ceil
p4java
viadeo
terminal-services
gnu-smalltalk
quantlib-swig
jsctypes
aqtime
gridfs
apc
sqlperformance
simba
dmoz
first-responder
runas
jspinclude
objective-c-2.0
jquery-ui-layout
hgsubversion
invite
thunderbird-lightning
scripting-languages
dbisam
lzh

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile