In this blog post, we will investigate Java 10′ s var feature and polymorphism concept. Let’s start with the question, What will the output of small code on the right side according to hierarchy on the left side.
Let’s explain the answer, Above code block will not compile, the error will be “Incompatible types” because if we connect Car object with “var” type (vehicle) then no polymorphic action are allowed in order to not get a runtime exception (ClassCastException).
If you would change var vehicle = new Car to Vehicle vehicle = new Car then again you can start using polymorphism. “var” feature in Java 10 comes with a price. You can download code from github.