What makes an object assignment-compatible with another class?

前端 未结 5 1342
借酒劲吻你
借酒劲吻你 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:16

    Thrown to indicate that the code has attempted to cast an object to a subclass of which it is not an instance. For example, the following code generates a ClassCastException:

     Object x = new Integer(0);
     System.out.println((String)x);
    

    more info here and here.

    0 讨论(0)
  • 2021-01-15 02:16

    Just wanted to add the official specification to support Ricardo's correct answer that "you can assign an object of type A to variable of type B if type A extends or implements type B":

    The JLS defines assignment-compatibility as follows:

    5.2. Assignment Contexts

    If the type of an expression can be converted to the type of a variable by assignment conversion, we say the expression (or its value) is assignable to the variable or, equivalently, that the type of the expression is assignment compatible with the type of the variable.

    The term "assingment conversion" is only defined as applying the appropriate conversion from the list given in the "Assignment Contexts"-chapter:

    The term "conversion" is also used to describe, without being specific, any conversions allowed in a particular context. For example, we say that an expression that is the initializer of a local variable is subject to "assignment conversion", meaning that a specific conversion will be implicitly chosen for that expression according to the rules for the assignment context.

    The most relevant for reference types being

    5.1.5. Widening Reference Conversion

    A widening reference conversion exists from any reference type S to any reference type T, provided S is a subtype of T (§4.10).

    Subtypes include implemented interfaces (see 4.10.2. Subtyping among Class and Interface Types ).

    There are additional rules for numeric and generic types, but they are not relevant for the example given in the question.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-15 02:27

    Consider these two cases:

    A a = new B();
    B b = (B)a;    <--  Ok.
    
    A a = new A();
    B b = (B)a;    <--  ClassCastException
    

    So in order to cast an object to B it must be an instance of B (or a subclass of B).

    In your case it is however an instance of A.

    0 讨论(0)
  • 2021-01-15 02:31

    The javadoc for Class.isInstance(Object obj) gives the definition of assignment compatible:

    Specifically, if this Class object represents a declared class, this method returns true if the specified Object argument is an instance of the represented class (or of any of its subclasses); it returns false otherwise. If this Class object represents an array class, this method returns true if the specified Object argument can be converted to an object of the array class by an identity conversion or by a widening reference conversion; it returns false otherwise. If this Class object represents an interface, this method returns true if the class or any superclass of the specified Object argument implements this interface; it returns false otherwise. If this Class object represents a primitive type, this method returns false.

    Basically, you can assign an object of type A to variable of type B if type A extends or implements type B.

    0 讨论(0)
提交回复
热议问题