From 7467be08a1918de0f87e28f1e098680abdc83c71 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Thu, 31 Oct 2019 14:57:28 +0100 Subject: [PATCH] is --- blog/java_6.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/blog/java_6.md b/blog/java_6.md index dd2f07a..c137d02 100644 --- a/blog/java_6.md +++ b/blog/java_6.md @@ -123,6 +123,8 @@ public class Main { ``` As you can see, I've expanded the code from before to have a `getTrucks` method, which iterates through every vehicle we have in stock and if it finds a vehicle that is an instance of the `Truck` class, it will print out a message. +Another way to think about the `instanceof` keyword is as a weird, clunky synonym to the word "is": When asking `vehicle instanceof Truck`, we're really asking "Is this vehicle a truck?" + Now that we know which of the vehicles we have in stock are trucks, it might also be useful to know the storage capabilities of all of those trucks. However, as you can see, the `i`th vehicle we're currently inspecting is always of typ `Vehicle`, as that's what we store in the `stock` list. That means that we won't be able to access any `Truck`'s `storageArea` variable. That's what the concept of *type casting* can be used for. If you have a variable of a certain type, but you know for a fact that it's actually an instance of a *child class* of that type, then you can change that variable to be of the child class' type instead like so: