• 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

Overriding methods with generics

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now, I have two classes both using generics:

The first is:



And the second is:



Now, when attempting to compile I get this error:

Lion.java:1: name clash: setName(T) in Lion<T> and setName(T) in Animal have the same erasure, yet neither overrides the other
public class Lion<T extends String> extends Animal{
^
1 error

Huh? Isn't it obvious that the method in "Lion" overrides "Animal." (Yes, I tried adding the @Override notation. It didn't work.)

 
Ranch Hand
Posts: 423
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Andrew Stallard wrote:
Huh? Isn't it obvious that the method in "Lion" overrides "Animal." (Yes, I tried adding the @Override notation. It didn't work.)


No, it isn't obvious.

It would be obvious if you declare class Lion in this way:
class Lion<T extends String> extends Animal <T>{ ....

In this case setName(T xxx) methods in both classes have the same type of argument
and setName in Lion overrides setName in Animal.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just for your information, since String is final you can remove the entire <T extends String> and simply use String. After all, the only class that matches T is String itself.
 
Andrew Stallard
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ireneusz Kordal wrote:

Andrew Stallard wrote:
Huh? Isn't it obvious that the method in "Lion" overrides "Animal." (Yes, I tried adding the @Override notation. It didn't work.)


No, it isn't obvious.

It would be obvious if you declare class Lion in this way:
class Lion<T extends String> extends Animal <T>{ ....

In this case setName(T xxx) methods in both classes have the same type of argument
and setName in Lion overrides setName in Animal.





Thank You,

That worked.
 
reply
    Bookmark Topic Watch Topic
  • New Topic