Adil has provided the section that determines this behaviour, but I'd like to explain why this behaviour is sensible.
bool ? val1 : val2
This is an expression. Expressions need to have a type that is determinable at compile time. This makes it faster, and catches errors sooner.
Now, if:
val is an instance of MyObject1
which extends SomeParent
and implements MyInterface
,
and val2 is and instance of MyObject2
which extends SomeParent
and implements MyInterface
How do we determine the compile-time type of this expression? We could try to find a common type between MyObject1
and MyObject2
. What's the most obvious solution? Do you call it a SomeParent
or a MyInterface
? and If they had 2 Interfaces, which one would you pick?
The problem is this is a mess, and would require some pretty contrived rules(and in fact there are more examples that would be less clear) that at the end of the day, would be less intuitive than the current definition.