views:

321

answers:

4

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

+12  A: 

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?

vodkhang
Okay... but this calls toString method as myAbstractClass is by default extends Object class... But if I use Interface instead of abstract class...it gives me same output...why it so..?
Siddhi
All objects either directly or indirectly, explicitly or implicity extends Object class. If you use an interface instead of an abstract class, then your anonymous classes will still implicitly extend Object.
emory
I already added a new, clearer explanation for anonymous class for interface. You should also look at the link, it is really good:)
vodkhang
Yes sir.. Thanks a ton. :)
Siddhi
Maybe because you are new and don't know the rule. But you can accept the answer
vodkhang
+4  A: 

On the first println() you are overriding the toString() method from the anonymous myAbstractClass instance, therefore you get the string returned by your overriden toString() method, that's the default behavior of the println() function.

On the second println(), you are not overriding the toString() method, so println() uses the default one (inherited from Object).

On a side note, try formatting your code correctly, 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";
            }
        }); 
    } 
}
Cesar
Okay... but this calls toString method as myAbstractClass is by default extends Object class... But if I use Interface instead of abstract class...it gives me same output...why it so..?
Siddhi
Because any object instance you create, even if it's from a class implementing a interface, extends from Object. http://java.sun.com/javase/6/docs/api/java/lang/Object.html
Cesar
+4  A: 

myAbstractClass is a minimal class. It inherits from object.

Class main constructs two anonymous inner classes from myAbstractClass, and prints their toString output.

  1. The inner class overrides the toString method and you see its output.
  2. The inner class get a method added, and you the the default toString definition.
BillThor
+1  A: 

In both scenarios, you have printed the object. So it will call toString() method of the object. In the first scenario, since you have overridden the toString() method, so it is printing myAbstractClass toString. In the second scenario, since it was not overridden, it calls the default toString() implemented in the Object class.

I think you are expecting function call in the 2nd scenario which is wrong. You have just overridden it but never called.

Sujee