1

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

Joy
  • 171
  • 2
  • 10
  • [Anonymous Classes](https://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html) - You are initializing an Object that implements the `FileFilter` interface. – Zachary Jan 23 '18 at 06:16
  • @Zachary, If am creating a class, then why its using Interface name with new ...? – Joy Jan 23 '18 at 06:19
  • "The syntax of an anonymous class expression is like the invocation of a constructor, except that there is a class definition contained in a block of code." - [Java Tutorials are great](https://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html) – Zachary Jan 23 '18 at 06:21

2 Answers2

2

To answer your questions, let's take one by one

1) new FileFilter() of which class object is being created here ?

It will be an object of anonymous class. See Can we create an object of an interface?

2) If we are using interface in class, then why its not implemented in above class ?

It does not require to implement from main class. You are just referring a interface in your class which does not have to be implemented.

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() ?

Yes in that case, all method needs to be implemented.

Kayaman
  • 72,141
  • 5
  • 83
  • 121
jeet427
  • 538
  • 3
  • 16
  • so you mean to say that terminology of implementing interface is same, the only difference here is anonymous class is being created which implements that Interface. And for creating a object of anonymous class which implements interface and whose reference is Interface type, syntax will be new ImplementedInterface() ...? Is it correct..? – Joy Jan 23 '18 at 06:31
  • To create an anonymous class object, you need to do `new ImplementedInterface(){all methods}`. – jeet427 Jan 23 '18 at 06:33
  • Is it creating a class or class automatically gets created first while writing this new FileFilter() ? Because listFiles accepts FileFilter type only ..! – Joy Jan 23 '18 at 06:36
  • Yes, it creates a anonymous class of type FileFilter. You may see more info at https://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html – jeet427 Jan 23 '18 at 06:40
  • What's a benefit of doing this ? We already have terminology to implement interfaces and use them ..! – Joy Jan 23 '18 at 06:56
  • An anonymous inner class can be useful when making an instance of an object of some type and overloading methods, without having to actually create a class. This should be done when that implementation is not required anywhere else. – jeet427 Jan 23 '18 at 07:03
  • So when it creates a object of Anonymous class, 1) Interface type can refer to that object..? 2) What is FileFilter() here..Is it a constructor ..? If not then what..? – Joy Jan 23 '18 at 09:08
0

Anonymous classes can implement interfaces, and that too without the "implements" keyword.

More stackoverflow links: Can we create an instance of an interface in Java? , Can we create an object of an interface?

On a side note, You have hit a case of Functional Interface which have been introduced in Java 8. An interface with exactly one abstract method is called Functional Interface.

Read more about functional interfaces here: https://www.journaldev.com/2763/java-8-functional-interfaces

gargkshitiz
  • 2,130
  • 17
  • 19