Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling super Method within Anonymous Class

What is the proper way to call the super method of ParentClass from an anonymous class?

In its current state super is referring to the Runnable.

public class ChildClass extends ParentClass {

    @Override
    public void myMethod(final double value) {

        new Runnable() {
            @Override
            public void run() {
                super.myMethod(value);
            }
        };
    }
}
like image 624
BAR Avatar asked Sep 24 '14 08:09

BAR


2 Answers

You can use the following code:

public class ChildClass extends ParentClass {
    @Override
    public void myMethod(final double value) {
        new Runnable() {
            @Override
            public void run() {
                ChildClass.super.myMethod(value);
            }
        };
    }
}
like image 76
Stefan Dollase Avatar answered Sep 28 '22 11:09

Stefan Dollase


call ChildClass.super.myMethod(value);

Note : You can also use ChildClass.this.myAttribute / ChildClass.this.myMethod() if you want to access instance attributes/methods from ChildClass.

like image 24
Arnaud Denoyelle Avatar answered Sep 28 '22 10:09

Arnaud Denoyelle