Skip to content

Instantly share code, notes, and snippets.

@fge
Last active December 18, 2015 09:38
Show Gist options
  • Select an option

  • Save fge/5762353 to your computer and use it in GitHub Desktop.

Select an option

Save fge/5762353 to your computer and use it in GitHub Desktop.
Anonymous overloading in Java
package com.github.fge.uritemplate;
public final class Test
{
public static class A
{
public static final int x = 1;
public int m(A a, B b)
{
return a.m(b, b) + x;
}
}
public static class B
extends A
{
public int m(A a1, B a2)
{
if (a1 == a2)
return x;
return super.m(a1, a2);
}
}
public static void main(final String... args)
{
B b1 = new B()
{
int m(B a, B b)
{
return 10;
}
};
System.out.println(b1.m(b1, b1)); // prints 1
System.out.println(new B() {
int m(B a, B b) { return 10; }
}.m(null, null)); // prints 10
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment