简体   繁体   中英

Java Generics - Unexpected bounds

No Problems here

public class MyList<E extends Number> extends ArrayList<E> {
}

.

Unexcepted bound. What does this mean? And why is it wrong? Thanks for help.

public class MyList<E extends Number> extends ArrayList<E extends Number> {

}

class MyList<E extends Number> is OK because you declare a type parameter, so you have to give it a name ( E ) and you can optionally declare it as bounded ( extends Number ).

In extends ArrayList<E> instead you just have to "use" a type parameter: with " <E> " you refer to the parameter declared in your class, for which a bound is already given in its declaration. " <? extends Number> " (with ? in place of E ) would also be accepted by the compiler (although it would be not what you want). Instead, " <E extends Number> " is taken as a type parameter declaration, so it is a compile error.

public class MyList<E extends Number> extends ArrayList<E> {
    private static final long serialVersionUID = -1025575227555594680L;

}

This should work with no compilation error and no warnings even. Let me know if you still have the same error.

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