I have seen we can pass any types of arguments in Method.
import java.io.File;
import java.io.FileFilter;
public class LastModified {
public static File lastFileModified(String dir) {
File fl = new File(dir);
File[] files = fl.listFiles(new FileFilter() {
public boolean accept(File file) {
return file.isFile();
}
});
long lastMod = Long.MIN_VALUE;
System.out.println("lastMod:"+lastMod);
File choice = null;
for (File file : files) {
System.out.println("File:"+file);
if (file.lastModified() > lastMod) {
choice = file;
lastMod = file.lastModified();
}
}
return choice;
}
public static void main(String[] args) {
lastFileModified("D:\\TestFiles");
}
}
Here in listFiles method we are passing Interface thing. It seems that Interface object is being created, but as far I know it cannot be done. It just refer the object of class which implements that interface.
Being said that "It's just a way of saying "this parameter will accept any object that supports this interface. It's equivalent to accepting some object of a base class type, even if you're passing in a subclass." NOT CLEARED
Questions:
1) **new FileFilter()** of which class object is being created here ?
2) If we are using interface in class, then why its not implemented in above class ?
3) If its a one more way to implement, then what if that interface would have 10 declared methods ? So Do we need to define all after **new FileFilter()** ?
Can anyone help me to understand this ? I'm really confused here