I found an interesting behavior when using polymorphism in Java lambda expr. I hope someone could shed some light on this.
Assume following things:
public class Java8Test {
public static void main(String[] args) {
A a = new B();
a.meth((z) -> System.out.println() );
}
}
interface X
{
void meth(int a);
}
interface Y
{
void meth(int a);
}
class A
{
void meth(X x)
{
System.out.println("A");
}
}
class B extends A
{
void meth(X x)
{
System.out.println("B");
}
}
If you run Java8Test.main , we get output as
B
We know the "a" refers to B's reference at runtime and so it prints "B" But when we change the method type in Class B as follows
class B extends A
{
void meth(Y y)
{
System.out.println("B");
}
}
If you run Java8Test.main , we get output as
A
We know the "a" refers to B's reference at runtime BUT , how come it prints "A" this time ??