| 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
|
|
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.
|
 |
 |
|
|
subject: Interface accepting instance
|
|
|