Java 10, var and polymorphism

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.

 

 

var” feature in Java 10 is not the same as in Javascript world.  The aim of “var” feature in Java is to simplify coding and reading.

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.

 

 

 

 

 

 

No Comments

Post a Comment

Comment
Name
Email
Website