public class A {
protected int x;
public A(int x) { this.x = x; }
public void g() { System.out.println(x); }
public void h() { System.out.println(x + 10); }
}
public class B {
public void f() {
(new A(2) {
public void g() {
h();
}
}).g();
}
}
public static void main(String[] args) {
new B().f();
}
Can some body help me understanding this line in code:
new A(2) { public void g() {h();} }).g();
I don’t understand if he define a anonymous class here with A ?? and how in the anonymous he can refer to A.h() ?
He creates an anonymous class and also overwrites
g(). It’s the same like when you useRunnableandoverriderun.means