Override Method with a Common Return Type

Say I have a superclass that defines the following abstract method

public abstract <T extends Interface> Class<T> getMainClass();

Now, if I want to override it in some subclass

public Class<Implementation> getMainClass(){
    return Implementation.class;
}

I get a warning about type safety and unverified conversion:

Security type: return type Class<Implementation>for getMainClass()from type SubFoorequires raw conversion to match Class<Interface>typeSuperFoo

Doesn't Class<Implementation>fall under Class<T>if <T extends Interface>? Is there a way to get rid of the warning correctly?

+5
source share
4 answers

the return type of the overriding method must be a subtype of the return type of the overridden method.

Class<Impl> Class<T>, <T extends Interface>. T .

Class<Impl> - Class<? extends Interface>, .


:

X

  • A<X> A<? extends X>

  • A<X> A<? super X>

S T

  • A<? extends S> A<? extends T>

  • A<? super T> A<? super S>

, (<: " " )

A<S>    <:    A<? extends S>    <:    A<? extends T>

A<T>    <:    A<?  super  T>    <:    A<?  super  S>
+11

, :

public class SuperFoo {
     public abstract <T extends Interface> List<T> getList();
} 

public class SubFoo extends SuperFoo {
     private List<Implementation> l = new ArrayList<Implementation>();

     public List<Implementation> getList() {
          return l;
     }

     public void iterate() {
          for (Implementation i: l) ...;
     }
}

SubFoo subFoo = new SubFoo();
SuperFoo superFoo = subFoo;
superFoo.getList().add(new AnotherImplementation()); // Valid operation!
subFoo.iterate(); // Unexpected ClassCastException!

ClassCastException.

, , Class<...>, ( ), :

@SuppressWarnings("unchecked")
public Class<Implementation> getMainClass(){ ... }  

- SuperFoo :

public class SuperFoo<T extends Interface> {
    public abstract Class<T> getMainClass(); 
}

public class SubFoo extends SuperFoo<Implementation> {
    public Class<Implementation> getMainClass() { ... }
}

(, , ) . .

+3

public abstract Class<? extends Interface> getMainClass();

java try

class OtherImpl implements Interface{ 
} 
A a = new B();//where A - your abstract class and B - implementation
Class<OtherImpl> other = a.<OtherImpl>getMainClass();//some broken think, But _without_ runtime exception

@axtavt . .

+1

- public abstract Class<? extends Interface> getMainClass();, public abstract Interface getMainClass();?

, Interface, , , getClass() .

, ,

public InterfaceImpl implements Interface {
    // ...
};

public abstract class A {
    public abstract Interface getMainClass();
    // ...
}

public class AImpl {
    return new InterfaceImpl();
}

public class Main {
    public static void main(String[] args) {
        AImpl aImpl = new AImpl();
        Interface i = aImpl.getMainClass();
        System.out.println(i.getClass());
    }
}
0

All Articles