Class.this.method overriding inheritance trying to understand
Hi I saw a piece of code that calls MyClass.this.aFunctionOverriddenByMeThatIsInMySuperClass ()
Im just looking for some clarity on how this works what function this actually calls and why someone would write code in this manner.
I have created a simpler version below
2 class A and B and there basic functionality is described in each
class A {
public A (){};
public int func1 () { return 1; }
}
class B extends A {
public B (){};
public int func1() { return 0; } //note this returns 0 where A.func1 = 1
public int myFunc() { return B.this.func1(); }
//public int myOtherFunc() { return A.this.func1(); } //NOT LEGAL wont compile
/**
B.java:5: not an enclosing class: A//pardon the stupidity but what is an enclosing class?
public int myOtherFunc() { return A.this.func1(); }
*/
}
class D extends B {
public D (){};
public int func1(){ return 4; }
}
class C
{
public static void main (String [] args)
{
A a = new A();
B b = new B();
D d = new D();
System.out.println(a.func1()); //prints 1
System.out.println(b.func1()); //prints 0
System.out.println(b.myFunc()); //prints 0
System.out.println(d.myFunc()); //prints 4
}
}
I guess my assumption is that using MyClass.this.someOverriddenFunction allows the subclass to have more control ?
any thought or comment would be much appreciated.
thanks

