• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do you extend a generic abstract class

 
Seema Kekre
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Code reference Generics Question


At Line 2, I tried
class SubABS<K> extends ABS<K>,
class SubABS<K extends Number> extends ABS<K extends Number>

and all variations of this, finally it compiles with class SubABS extends ABS, and the warning that raw types are used.

When I use Eclipse to implement the unimplemented methods, I get the method on 3. When I used the exact signature in 1, I get an error
Multiple markers at this line
- Name clash: The method useMe(ABS<? extends K>) of type SubABS has the same erasure as useMe(ABS) of type
ABS but does not override it
- The method useMe(ABS<? extends K>) of type SubABS must override a superclass method

How can I extend this abstract method correctly using Generics and why am I getting the above issues?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Seema Kekre
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now it all compiles fine.
I infer that ABS<K> is required because we have already defined what K is in the abstract class, then why do I need to redefine K for ABS2?
 
Leon Omk
Ranch Hand
Posts: 75
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seema Kekre wrote: Now it all compiles fine.
I infer that ABS<K> is required because we have already defined what K is in the abstract class, then why do I need to redefine K for ABS2?


I have the same question.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to tell which class you are actually using for ABS. If you are not using any generic type in ABS2, you can also define the class like :

or refine it to Double for example:
 
Seema Kekre
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So either you use ABS to define the type of the subclass if you dont want the subclass to use Generics, or use Generics wrt K's definition of the superclass. Right?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic