Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8040527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:55:34+00:00 2026-06-05T03:55:34+00:00

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

  • 0

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);
    }     
}  
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T03:55:35+00:00Added an answer on June 5, 2026 at 3:55 am

    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)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have got below code which is working fine: public abstract class Beverage {
I have got below code to GET dictionary type of session variable value. Please
I got below mentioned code for adding my application to Windows Firewall Exception list.
I got below code from http://msdn.microsoft.com/en-us/library/dd584174(office.11).aspx for adding custom property in webpart tool pane.
I got below code in my Word Document (office 2007) to send a mail
I've got the code below, and I'm trying to set the from field to
I got the below piece of code and it keeps on printing the frequency
I have got below code in which I am taking html and then reading
I want to pick photo from contacts . I got below code from stack
I have got below code in c#. SqlConnection conn = new SqlConnection(Data Source=MANOJ-PC\\SQLEXPRESS;Initial Catalog=master;Integrated

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.