java - Calling Methods of Anonymous Inner Class in the parent class -


i got below doubt when surfing anonymous inner class

here original code downloaded , working around (please refer below code questions).

as per link above cannot overload & add additional methods in anonymous inner class.

but when compile below working fine though not able call public methods outside inner class.

initially surprised why not access public methods outside inner class realized object being held "father" class reference not know such function call.

what changes can make in below code making calls overloaded method , new method outside 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 , executes fine.             newmethod(); //this compiles , executes fine.         }          // overload method in superclass father         public void method(string str) {             system.out.println("anonymous: " + str);         }          // adding new method         public void newmethod() {             system.out.println("new method in anonymous");             someothermethod(); //this compiles , executes too.         }          };          //fanon.method("new number");  // compile error         //fanon.newmethod();         // compile error - cannot find symbol      }      public static final void someothermethod()     {         system.out.println("this in 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 anonymous class; conflicts java's static type system. conceptually, variable fanon of type father, has no .method(string) or .newmethod methods.

what want ordinary (named) subclass of father:

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

and should declare new variable

fathersubclass fanon = new fathersubclass(d) 

Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -