What you described is generally impossible. However, if a subclass returns a "narrower" subtype of the return method of the superclass, this is called a "covariant return type" and is allowed in Java with JDK 1.5. However, based on your example, I don't think covariant return is what you are looking for.
I guess you want
for (A a : allAs) { String b = a.getValue(); int c = a.getValue(); }
The problem here, of course, is that the compiler does not know at compile time which of these two statements is correct, and they cannot be true.
source share