What makes an object assignment-compatible with another class?

前端 未结 5 1386
借酒劲吻你
借酒劲吻你 2021-01-15 01:43

I\'m wondering what specifically allows an object of one class to be cast as another class. Looking at the Class.isInstance(Object obj) javadoc, it suggests that an object

5条回答
  •  天命终不由人
    2021-01-15 02:19

    If we replaced A and B with meaningful names that help us think about the problem, then the answer becomes clearer. If A becomes Mammal and B is Dog, then we can reasonably say that all Dogs are Mammals, but we cannot say that all Mammals are Dogs. Even if they superficially shared the same attributes, there is no guarantee that all Mammals would fulfill the contract of being a Dog, and the compiler shouldn't try to assume so.

提交回复
热议问题