Inheritance and test result "Instanceof"

I am learning "instanceof" java, but I could not understand "instanceof", I thought below answer would be true and false, but the result is correct. Could you please explain why this result is happening? As I know when A is a child of B (parent) and instance B is "false", but the result is different from what I thought.

class Car{
    String color;
    int door;       
}

class FireEngine extends Car{
    void water(){
        System.out.println("water");
    }
}

public class Operator {
    public static void main(String[] args) {
        Car car = new FireEngine();
        FireEngine fireCar = new FireEngine();

        System.out.println(car instanceof FireEngine);
        System.out.println(fireCar instanceof Car);
    }
}

      

+3


source to share


5 answers


Declaration! = Value

You advertise car

as a car, but a value FireEngine

.

instanceof

works based on values, not on their variable declarations !!!



An abbreviation can help you understand:

System.out.println(new FireEngine() instanceof FireEngine);  // true
System.out.println(new FireEngine() instanceof Car);         // true

      

+7


source


The output instanceof

depends on the runtime type of the variable you are testing. The compile-time type of the variable does not matter (as long as it is possible for x instanceof

Y to return true for some value x

, otherwise the expression will fail compilation).



Both car

and fireCar

include copies FireEngine

in your code. And as FireEngine

a kind of a car

, then car

, and fireCar

it is instanceof

like car

, and FireEngine

, therefore, your code prints true

and true

.

+1


source


Instanceof operator implementation. Returns a Boolean value if the Object parameter (which can be an expression) is an instance of the class type.

Input 1: an object or expression that returns an object.

Input 2: a class or expression that returns a class Returns: A Boolean value that is the result of testing an object against a class.

For more information go to javadocs @ http://docs.oracle.com/cd/E13155_01/wlp/docs103/javadoc/com/bea/p13n/expression/operator/Instanceof.html

For a more detailed explanation with examples, visit the following web page: http://mindprod.com/jgloss/instanceof.html

+1


source


There are two types of bindings in Java: Static (reference type) and dynamic (object type).

In your case:

 Car car = new FireEngine();

      

Car is a static type and FireEngine is a dynamic type. This means that you are actually working with a FireEngine (type object ). You can imagine the Car as a special sign with the car shape pointing to the real object, which is your amazing Fire Earth. If you are reading "instanceof" you can understand this, this method tells you that an object is an instance of a class and not a reference. So the compiler will see: FireEngine (car) instanceOf FireEngine? Of course, let the truth come back!

You can also have a look at this post: What is the 'instanceof' operator used for?

+1


source


Statement

As I know, when A is child of B (Parent), 
and a instanceof B is 'false' but result is different with what I thought.

      

wrong. instanceof

doesn't check for the existence of a child, it checks for the parent.

+1


source







All Articles