On the problem of transforming a superclass into a subclass

the superclass is converted to a subclass and should be checked with instanceof. That is,
if (superclass instanceof subclass)
{convert}

but does the superclass instanceof subclass all return false? Isn"t it impossible to convert in this way?

Feb.28,2021

this comparison is not a comparison between classes, but a comparison between the declared type of a variable and the actual type. For example, An is the parent class (or superclass) of B, and a variable an is declared as type A, but it may actually be type B. The purpose of an instanceof B is to find out whether an is type B.


you have a wrong understanding of instanceof . You can refer to the answer of fabricated belief. The concept of class and instance is also a bit confused., an instanceof B is to find out whether instance an is type B in the end. You can take a look at the concept of "polymorphism" in object-oriented


instanceof to check the value type. Even if that value comes from an Object variable, you still get true for that true.


if it is to declare a type, inheritance is not recommended, but interface.

Menu