• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Type erasure.

 
Ranch Hand
Posts: 160
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


can anyone please tell me why this code is not working ??? and one another question i have in my mind regarding this is with what compiler will set the type of the object g when the code is compiled ???
 
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ankur trapasiya wrote:can anyone please tell me why this code is not working ???



ItDoesntWorkIsUseless.

ankur trapasiya wrote:and one another question i have in my mind regarding this is with what compiler will set the type of the object g when the code is compiled ???



Gen1. That is the type of the variable and of the class instantiated. The generic type parameter is lost; if it would be available it would be "A" for the object, "? extends A" for the variable. That is what the compiler checks for, but not the runtime environment; thus "type erasure".
 
ankur trapasiya
Ranch Hand
Posts: 160
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry i am not getting what you want to say .... I am just saying that it should work because B is-a A. But it is giving compiler error.
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you considered reading the compiler error to see what it says?

(I assume you already read the ItDoesntWorkIsUseless link; now would be the time to read the TellTheDetails link.)
 
ankur trapasiya
Ranch Hand
Posts: 160
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
D:\java\scjp\SCJPEXAM\Collections\GenericsX>javac GenericTest.java
GenericTest.java:33: doE(capture#774 of ? extends A) in Gen1<capture#774 of ? ex
tends A> cannot be applied to (B)
System.out.println(g.doE(new B()));
^
1 error


i am not understanding this error that's why i posted question here..
 
Hauke Ingmar Schmidt
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Generics are complicated sometimes.

Just think of the following declaration instead of yours:


Would it be legal? - Sure, C is a subclass of A.

But now the concrete type of g is parametrized to C, not A or B.

So what would happen when this line is called?



Oops. The (concrete) method signature is "doE(C)". A "B" is-not a "C".

That is why the compiler won't let you call the method.

The problem is the declaration "? extends A". It is a lower bound for the variable, but not the object. The referenced object can be parametrized to anything else that is subtype. So the compiler can't allow you to call the object with any type at all.

As this can get confusing you should avoid bounds with variables.
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought I got what Hauke explained, and then I tried this code where I modified Line 1 as indicated:

No compile error. No runtime errors. Except for a warning saying "The type parameter E is hiding type E" at Line 1. Can someone explain this?
 
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ankur,

Please post the source when you are posting code.

Thanks
 
ankur trapasiya
Ranch Hand
Posts: 160
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Hauke : i think i got your point ... may be compiler prevents this to stop runtime errors because any of the child of A may come regardless of inheritance hierarchy.....
@seema : yeah i have tested it ... It's working perfectly ...

@Janeice : source is my self... i was testing my generics concepts ...

 
Hauke Ingmar Schmidt
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Seema Kekre wrote:
No compile error. No runtime errors. Except for a warning saying "The type parameter E is hiding type E" at Line 1. Can someone explain this?



You are introducing a method-local type parameter in line 5. This parameter E has nothing to do with the class's type parameter in line 4. As they have the same name E you can't access the class's parameter any more, it is locally hidden within the method. Every reference to E in that method (and same line) now access the method's type parameter (which is distinct from the class's type parameter).

Just rename the method's type parameter and all it's references. The result is certainly not what you wanted, but it is not now, after renaming it is obvious.
 
Seema Kekre
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Hauke. I think I get it now.
 
reply
    Bookmark Topic Watch Topic
  • New Topic