Can someone explain the operation of the following code ...?

Can someone explain the operation of the 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";}}); } } 

Exit...

 myInterfacetoString primitivedemo.Main$2@9304b1 

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

again ... if these are anonymous classes, then the main class should allow me to give some name to these anonymous classes .. But if I try to do this ... I get a compilation error

+6
java interface abstract-class anonymous-class
source share
4 answers

When you print an object, it calls the toString () method. In the first statement, you create a new object, as well as an override method toString. Therefore, this toString () method is called when the object is printed.

In the second expression, you also create an object, but you do not override the toString () method to use the toString () method of the Object class.

For a 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 creates 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 defined by the Body class.

The result of this expression is that a new class is defined that extends className , an instance of a new object of the new class is created, and the expression is replaced with a reference to the new object .

 new interfaceName(){classBody} 

This expression creates a new object from an unnamed and previously undefined class that automatically implements an interface named interfaceName and automatically extends a class called Object . A 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 the Body class.

Is this clear to you now?

+14
source share

In the first println() you override the toString() method from an anonymous instance of myAbstractClass , so you get the string returned by your overriden toString() method, which is the default behavior of println() .

In the second println() you do not override the toString() method, so println() uses the default value (inherited from Object ).

On the side of the note, try formatting your code correctly, it is much easier to read and understand.

 abstract class myAbstractClass{} public class Main { public static void main(String[] args) { System.out.println(new myAbstractClass(){ public String toString(){ return "myAbstractClass toString"; } }); System.out.println(new myAbstractClass(){ public String myFunction(){ return "myAbstractClass myFunction"; } }); } } 
+3
source share

myAbstractClass is a minimal class. It inherits the object.

The main class builds two anonymous inner classes from myAbstractClass and outputs their output toString.

  • The inner class overrides the toString method, and you see its output.
  • The inner class gets the added method, and you define the default toString definition.
+3
source share

In both scenarios, you printed an object. Therefore, it will call the toString() method of the object. In the first scenario, since you redefined the toString() method, so it prints myAbstractClass toString . In the second scenario, since it was not overridden, it calls the default value toString() implemented in the Object class.

I think you are expecting a function call in the second scenario, which is wrong. You just redefined it, but you never called.

+1
source share

All Articles