Following a review of the var
feature as seen here:
I have encountered difficulties with setting up my Eclipse/IntelliJ IDEA IDEs with JDK 10 and am therefore asking help from Stack Overflow users who have a working Java 10 environment.
Consider the following:
public class A {
public void someMethod() { ... }
}
public class B extends A{
@Override
public void someMethod() { ... }
}
...
...
...
var myA = new A(); // Works as expected
myA = new B(); // Expected to fail in compilation due to var being
// syntactic sugar for declaring an A type
myA = (A) (new B()); // Should work
myA.someMethod(); // The question - which someMethod implementation is called?
When using var
, I expect the JVM to recognize the deriving class type which the variable holds. And to execute B:someMethod() instead of A:someMethod() when executing myA.someMethod().
Is this indeed the case?