theAnonymous
theAnonymous

Reputation: 1804

How to cast correctly?

protected final Class<? extends MyBaseClass>[] getClasses(){
    return new Class[]{
        MyClass1.class,
        MyClass2.class
    };
}

Now, as the dev, I know that MyClass1 and MyClass2 extends MyBaseClass, but how do I do it properly so that the IDE/compiler won't give warning?

Upvotes: 2

Views: 62

Answers (1)

ernest_k
ernest_k

Reputation: 45339

You can't get there without suppressing warnings. You either have to deal with the compile-time error preventing the creation of an array of a generic type, or the current warning.

The closest alternative is to make your method return a collection type:

protected final List<Class<? extends MyBaseClass>> getClasses() {
    return Arrays.asList(MyClass1.class, MyClass2.class);
}

Upvotes: 1

Related Questions