• Post Reply Bookmark Topic Watch Topic
  • New Topic

Instantiating an Interface  RSS feed

 
Red Smith
Ranch Hand
Posts: 136
1
Netscape Opera Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The File class can take a FilenameFilter interface for it's list() method.

You can create a class for the list() function like:



What is the class called that get's instantiated there? I didn't think you could instantiate an interface so the syntax looks strange.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can only "instantiate an interface" if you supply implementations for all its methods. What actually gets instantiated is an anonymous class that happens to implement this interface. If you compile this code, you'll see that there's an extra class file being created called something like "Test$1.class" (assuming that this code is part of the Test class) - Test$1 is the anonymous class that implements the interface and which gets instantiated. So this notation is merely a syntactic shortcut so you don't have to declare an actual class.
 
Red Smith
Ranch Hand
Posts: 136
1
Netscape Opera Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ulf.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!