• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Generic return type questions

 
Ranch Hand
Posts: 472
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When i try some code, i found strange (for me) behaviour:


compiler error wrote: error: ment(List<String>) in B cannot override ment(List<String>) in A public List<Integer> ment(List<String> a){ return type List<Integer> is not compatible with List<String>


thats looks not strange, next i edit class B ment parameter to List a

unchecked warning wrote:uses unchecked or unsafe operations.


but code compile ok - and i think this is strange but my first opinion than if i also change class A ment parament to List a code will compile to, but i was wrong

compiler error wrote:ment(List) in B cannot override ment(List) in A public List<Integer> ment(List a){ return type List<Integer> is not compatible with List<String>


_____________________
My question is why second example compile ok and why third not compile?
 
Ranch Hand
Posts: 94
1
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In second example you have used argument List without generics which is equals to List<?>.
That means you have overloaded method instead overriding,so it compiles.

Where as in other two example you are overriding where return type must match to parent method.
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree Sergej, this does seem a bit weird. If you mix typed and raw parameters, then compiler is ignoring the incompatible typed return types. Don't know what the reason is, but it's not intuitive...
 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gajendra Kangokar wrote:That means you have overloaded method instead overriding,so it compiles.


Gajendra, that's what I initially thought as well. But I tried adding Override annotation to be sure and it is indeed overriding.

Since both methods have the same erasure, they can't be overloaded. Following gives error:
 
Gajendra Kangokar
Ranch Hand
Posts: 94
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes right


In rum time the above code becomes



we dont want code that compiles fine but gives error at runtime so compiler does not allow to have such methods
 
The only thing that kept the leeches off of me was this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic