Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

generic interface and static generic method

 
Bruno Sant Ana
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

I'm studying for Java exam and I saw in a book that we can have a generic interface and a static generic method as well. I tried to code it in order to learn more about that. See my code:




This code compiles, but when I try to replace the concatenarTextos method by this one bellow it throws an exception:



this exception is thrown:

java.lang.ClassFormatError: Duplicate field name&signature in class file certificacao/generics/ImplementacaoGenerica

why does it occurs?
 
Matthew Brown
Bartender
Posts: 4566
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java has "type erasure", which means that generic information doesn't exist at runtime. It's only used by the compiler. So the following signatures:
both compile to the signature:
which means you get a conflict.
 
Bruno Sant Ana
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, but I'm not trying to use both at the same time like this:


I'm trying to compile only this method bellow:


Here is all the code:
 
Henry Wong
author
Marshal
Pie
Posts: 21000
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bruno Sant Ana wrote: ... but when I try to replace the concatenarTextos method by this one bellow it throws an exception:



this exception is thrown:

java.lang.ClassFormatError: Duplicate field name&signature in class file certificacao/generics/ImplementacaoGenerica

why does it occurs?


Well, both "T" and "U" can be String objects (or can be Object objects -- the super of String). Question: assuming that they are *not* string objects, then how should the compiler perform the "t+u" operation?

Henry
 
Bruno Sant Ana
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but even if I change to:



it doesn't compile. Why?
 
Henry Wong
author
Marshal
Pie
Posts: 21000
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bruno Sant Ana wrote:but even if I change to:



it doesn't compile. Why?


Hint: what happens when you cast it (the parameter to the println() method) to an Object type?

Henry
 
Matthew Brown
Bartender
Posts: 4566
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bruno Sant Ana wrote:OK, but I'm not trying to use both at the same time...

Ah, sorry, I misunderstood.

The reason you get an error (and I get a different error to the one you reported) is that super isn't allowed as a bound on a type variable. You can have lower bounds on wildcard type arguments, but not where you've used them.

The relevant parts of the Java Language Specification are http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.4 and http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.5.1. Look at the definition for a type variable - it doesn't mention super. As to why there's a difference? Well, there's a reference to that in the following line:

Unlike ordinary type variables declared in a method signature, no type inference is required when using a wildcard. Consequently, it is permissible to declare lower bounds on a wildcard, using the following syntax, where B is a lower bound:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic