You change the signature of the foo method in a way that is not supported. Polymorphism works only for different lists of arguments, and not for identical methods that differ only in the type of return value.
And if you think about it, itβs quite natural ... If it works, and someone who knows only about one of the two superclasses calls Three.foo (), he expects it to return One (because it's like this works in One and Two), but in Three you really can bring back the HashMap and still behave correctly.
Jon (in the comment below) is correct, you can narrow the scope, but then you will still follow the protocol in which you will return βOneβ (if you return βThree from Three.foo ()), because subclasses will be all implement the superclass interface, however, the return type is still not part of the polymorphism, so you cannot have three different methods that differ only in the type of return value.
source share