简体   繁体   中英

How do I set the type of a generic superclass in Java?

Say I have a generic class of type E, and I want a subclass of that generic that always is the type MyObject. So the Generic class can still be used for other objects, but the subclass NonGeneric is always of type MyObject and has MyObject specific functions.

Ex:

public class Generic <T extends Comparable <T>>

And what I want is some

public class NonGeneric<MyObject> extends Generic<T>

Answer:

I did not properly flesh out my type that I was trying to use, and had extended the Comparable interface instead of Comparable<MyObject>

The reason this is the answer to the above question was that the fact that my non-generic type object that I wished to insert didn't correctly implement Comparable prevented me from declaring the non-generic class, throwing an error. I was just too confused to consider the basics.

The correct declaration, once my underlying bug was fixed, is

public class NonGeneric extends Generic<MyObject>

Where

MyObject extends Comparable<MyObject>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM