开发者

Can a method in an inner class access a parent class method?

开发者 https://www.devze.com 2023-02-27 04:20 出处:网络
I\'m not sure if my question title de开发者_JAVA百科scribes my situation aptly, so my apologies if it doesn\'t! Anyway, let\'s say I have the following code snippet (visibility is as stated):

I'm not sure if my question title de开发者_JAVA百科scribes my situation aptly, so my apologies if it doesn't! Anyway, let's say I have the following code snippet (visibility is as stated):

public class ChildClass extends ParentClass {
    // more code
   private void myMethod() {
      MyClass mine = new MyClass() {
         public void anotherMethod() {
            // insert code to access a method in ParentClass
         }
      };
   }
}

Is it possible for code within anotherMethod() to access a protected method found in ParentClass? If so, how can this be done?

I've tried something like...

(ParentClass.this).parentMethod();

...but obviously it doesn't work due to scope issues.


This compiles fine:

class MyClass {
}

class ParentClass {
    protected void parentMethod() {
    }
}

class ChildClass extends ParentClass {
    private void myMethod() {
        MyClass mine = new MyClass() {
            public void anotherMethod() {
                parentMethod(); // this works
            }
        };
    }
}


A non-static inner class can access all methods of the enclosing class as if it were it's own methods:

public class Test {
    public int getOne() {
        return 1;
    }

    public class Inner {
        public int getEnclosingOne() {
            return getOne(); // this works...
        }
    }
}

A static inner class can not, as a static inner class is not bound to an instance of the parent class. That can only call static methods on the enclosing class.

As for methods when taking into account inheritance, an method in a non-static inner class can use all the methods of the enclosing (outer) class.

The interesting part is Test2.super.getOne() which indeed obtains getOne() from Test2.super, which is a Test. This is just like Test2 would access the method, namely using super though prefixed with Test2 to indicate you're accessing the namespace of the outer class.

public class Test2 extends Test {

    public int getInnerOuterParentOne() {
        Inner2 inner2 = new Inner2();
        return inner2.getOuterParentOne();
    }
    public int getInnerOuterOne() {
        Inner2 inner2 = new Inner2();
        return inner2.getOuterOne();
    }

    public int getOne() {
        return 2;
    }

    public class Inner2 {
        public int getOuterOne() {
            return getOne();
        }
        public int getOuterParentOne() {
            return Test2.super.getOne();
        }
    }

    public static void main(String[] args) {
        Test2 test2 = new Test2();
        System.out.println(test2.getInnerOuterOne()); // 2
        System.out.println(test2.getInnerOuterParentOne()); // 1
    }
}


There is no way to access "parent class method" in Java, irrelatively to visibility (except for super.parentMethod() in subclass's parentMethod()).

That is, if ChildClass overrides parentMethod(), there is no way to call ParentClass.parentMethod() (bypassing ChildClass.parentMethod()) from other methods of ChildClass.

However, if ChildClass doesn't override parentMethod(), that method is inherited by ChildClass, so that you can access it as a ChildClass's method, i.e. simply as parentMethod().

0

精彩评论

暂无评论...
验证码 换一张
取 消