aspose file tools
The moose likes Beginning Java and the fly likes Interface accepting instance Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Interface accepting instance" Watch "Interface accepting instance" New topic
Author

Interface accepting instance

Mohan Vinukonda
Ranch Hand

Joined: Jul 28, 2005
Posts: 32
From the following code, interface is accepting creating new instance compiler is not giving any warning or error. Can anybody explain how it works. Any body help appreciated.

interface TestA { String toString(); }
public class Test {
public static void main(String[] args) {
System.out.println(new TestA() {
public String toString() { return "test"; }
});
}
}

-Mohan
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32611
    
    4
You are not instantiating the interface. You have implemented the single method it has, which is by the way implemented in java.lang.Object anyway.
So what you are instantiating is an anonymous inner class.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Interface accepting instance
 
Similar Threads
toString is overrides Object Class or TestA
Regarding interface
doubt in interface
Calling interface ?
constructor question