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 822113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T02:45:07+00:00 2026-05-15T02:45:07+00:00

Can anybody explain the working of following code…? interface myInterface{} public class Main {

  • 0

Can anybody explain the working of following code…?

interface myInterface{}

public class Main {

    public static void main(String[] args) {

        System.out.println(new myInterface(){public String toString(){return "myInterfacetoString";}});

        System.out.println(new myInterface(){public String myFunction(){return "myInterfacemyFunction";}});
    }
}

Output is…

myInterfacetoString
primitivedemo.Main$2@9304b1

All answers saying that myInterface in println() statement is anonymous class. But as I already declared it as an interface, why does it allow me to create anonymous class of same name….?

again…if these are anonymous classes then class main should allow me to give any name to these anonymous classes..But if try to do so..I’m getting compilation error

  • 1 1 Answer
  • 2 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-05-15T02:45:08+00:00Added an answer on May 15, 2026 at 2:45 am

    When you print out an object, it will call the toString() method. In the first statement, you create new object and also override method called toString. Therefore, this toString() method is called when object is printed.

    In the second statement, you also create an object but you don’t override the toString() method so that it will use the toString() method of the Object class.

    For the new question, this link has a good explanation for your understanding:
    Anonymous Classes

    Here is a copy of the explanation:

    new className(optional argument list){classBody}
    

    This expression instantiates a new object from an unnamed and previously undefined class, which automatically extends the class named className, and which cannot explicitly implement any interfaces. The body of the new class is given by classBody.

    The result of executing this expression is that a new class that extends className is defined, a new object of the new class is instantiated, and the expression is replaced by a reference to the new object.

    new interfaceName(){classBody}
    

    This expression instantiates a new object from an unnamed and previously undefined class, which automatically implements the interface named interfaceName, and automatically extends the class named Object. The class can explicitly implement one, and only one interface, and cannot extend any class other than Object. Once again, the body of the new class is given by classBody.

    Is it clear for you now?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anybody explain me why below code is working on private member variable? public
Can anybody explain what does JdClient is in this code: public JdClient client =
I am new in MVC and working on MVC 3. Can anybody explain what
Can anybody explain how this is working? RewriteRule ^update_my_account/([0-9]+)/([0-9]+)/?$ /index.php?page_id=$1&updated=$2 [L] What part of
Can anybody explain how this function works? public int TestAdd(int a,int b) { if(a
can anybody explain C C++ Java regarding dynamic typed or static typed language. I
Can anybody explain to me the cause of the following message, perhaps along with
Can anybody explain why pack() isn't working on this JFrame? It's got one JPanel
Can anybody explain, why next js code rises two alert windows with 'string1' text
Can anybody explain to me how objects are stored and removed from heap memory

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.