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

asked Jan 23, 2018 at 6:11
3
  • Anonymous Classes - You are initializing an Object that implements the FileFilter interface. Commented Jan 23, 2018 at 6:16
  • @Zachary, If am creating a class, then why its using Interface name with new ...? Commented Jan 23, 2018 at 6: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 Commented Jan 23, 2018 at 6:21

2 Answers 2

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
73.8k6 gold badges91 silver badges128 bronze badges
answered Jan 23, 2018 at 6:19
Sign up to request clarification or add additional context in comments.

7 Comments

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..?
To create an anonymous class object, you need to do new ImplementedInterface(){all methods}.
Is it creating a class or class automatically gets created first while writing this new FileFilter() ? Because listFiles accepts FileFilter type only ..!
Yes, it creates a anonymous class of type FileFilter. You may see more info at docs.oracle.com/javase/tutorial/java/javaOO/…
What's a benefit of doing this ? We already have terminology to implement interfaces and use them ..!
|
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

answered Jan 23, 2018 at 6:27

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.