简体   繁体   中英

Calling Methods of Anonymous Inner Class in the parent class

I got the below doubt when am surfing about Anonymous inner class

Here is the Original code I downloaded and was working around it (please refer to the below code only for my questions).

As per the link above they say we cannot overload & add additional methods in a Anonymous Inner class.

But When I compile the below it was working fine though I was not able to call those public methods outside the Inner class.

Initially I was surprised why I could not access the public methods outside the Inner class but then I realized the Object is being held by "father" class reference which does not know such function call.

What changes Can I make in the below code for making calls to the overloaded method and new Method outside the Inner class?

class TestAnonymous
{    

    public static void main(String[] args)
    {      
        final int d = 10;

        father f = new father(d);

        father fAnon = new father(d){                
        // override method in superclass father
        void method(int x){     
            System.out.println("Anonymous: " + x);
            method("Anonymous: " + x); //This Compiles and executes fine.
            newMethod(); //This Compiles and executes fine.
        }

        // overload method in superclass father
        public void method(String str) {
            System.out.println("Anonymous: " + str);
        }

        // adding a new method
        public void newMethod() {
            System.out.println("New method in Anonymous");
            someOtherMethod(); //This Compiles and executes too.
        }

        };

        //fAnon.method("New number");  // compile error
        //fAnon.newMethod();         // compile error - Cannot find Symbol

    }

    public static final void someOtherMethod()
    {
        System.out.println("This is in Some other Method.");
    }

} // end of ParentClass

class father
{   
    static int y;

    father(int x){ 
        y = x;
        this.method(y);
    }

    void method(int x){
        System.out.println("integer in inner class is: " +x);
    }     
}  

You cannot do this with an anonymous class; it conflicts with Java's static type system. Conceptually, the variable fAnon is of type father , which has no .method(String) or .newMethod methods.

What you want is an ordinary (named) subclass of father :

class fatherSubclass extends father
{ /* ... */ }

and you should declare your new variable

fatherSubclass fAnon = new fatherSubclass(d)

What changes Can I make in the below code for making calls to the overloaded method and new Method outside the Inner class?

Just make it not an anonymous class. You could declare the class within the method:

public static void main(String[] args) {
    class Foo extends Father {
        ...
    }
    Foo foo = new Foo();
    foo.method("Hello");
}

... but I'd probably recommend making it a separate class, either nested within the outer class if necessary, or just a new top-level class.

As soon as you start wanting to do anything complicated with anonymous classes, it's generally better to break it into a fully-fledged named class.

You can not call the 'overloaded' and new method from outside the anonymous class. You can call them inside your anonymous class, but never from outside. The outside world simply does not know of them. There is no class or interface specification that has information about them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM