aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How do you extend a generic abstract class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "How do you extend a generic abstract class" Watch "How do you extend a generic abstract class" New topic
Author

How do you extend a generic abstract class

Seema Kekre
Ranch Hand

Joined: Dec 02, 2010
Posts: 35
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?


OCPJP 6
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16



[My Blog]
All roads lead to JavaRanch
Seema Kekre
Ranch Hand

Joined: Dec 02, 2010
Posts: 35
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

Joined: Aug 17, 2010
Posts: 75

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.


OK, so that other guy knows Java better than I do, but I bet he can't speak Wuhanese(a Chinese Dialect) like me.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

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

Joined: Dec 02, 2010
Posts: 35
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?
 
 
subject: How do you extend a generic abstract class