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

[1528 byte] By [milokowica] at [2007-11-27 3:21:23]
# 1

Do you know what inner classes are? Inner classes are classes that are defined within another class. If an inner class is not declared as static, it needs an instance of the class it is defined within to exist, this is the enclosing class. MyClass.this can be used so that the inner class can explicitly invoke methods on it's enclosing instance

georgemca at 2007-7-12 8:24:07 > top of Java-index,Java Essentials,Java Programming...
# 2
one further commentif i make a class E that extends B without overriding func1 then e.myFunc returns 0 i guess i want to know the programming advantages of allowing an overridden method to have an effect on a piece of your code.
milokowica at 2007-7-12 8:24:07 > top of Java-index,Java Essentials,Java Programming...
# 3
i knew it was a stupid question when i wrote it.. just couldnt remember the term.. thanks for your answer though
milokowica at 2007-7-12 8:24:07 > top of Java-index,Java Essentials,Java Programming...