Shouldn't I be able to polymorphically access the class...
You could — if the type of your variable were of type C
. But it isn't, it's of type MyInterface
, and that completely defines what features of the instance you have access to. Yes, the instance has more features, but code written to the interface doesn't know that.
Adding features to C
has little to do with polymorphism. In this case, the polymorphism (there are a couple of different kinds of it) is that A
and C
could implement MyInterface
's features in different ways, but your code using instances via a MyInterface
reference neither knows nor cares that the instances are A
s or C
s; all it knows, all it should know, is that they claim to be faithful implementations of MyInterface
.
Let's take a concrete example:
public void doSomethingWith(List list) {
// ...
}
When something calls doSomethingWith
, it passes in an instance that fulfills the List
interface. That's all that that method knows about the instance (absent things like instanceof
and getClass
). The instance could be an ArrayList
, a LinkedList
, a Stack
, etc.; but the code using it doesn't know or care about that. All it knows is that it's a List
. Even if the method is passed an ArrayList
, it can't use (say) ensureCapacity
without first downcasting the reference (which will fail if the instance isn't an ArrayList
), because not all List
implementations have ensureCapacity
.
C
fromMyInterface
. – T.J. Crowder