3

For example, if I have an interface:

interface Executable {
    void execute();
}

You wouldn't be able to explicitly instantiate it. If you tried to create a new instance of the interface Executable, you'd get an compile error, saying 'cannot instantiate the type Executable'.

However, when trying to pass an interface to a method, I realized that you could do this:

class Runner {
    public void run(Executable e) {
        System.out.println("Executing block of code...");
        e.execute();
    }
}

public class App {
    public static void main(String[] args) {
        Runner r = new Runner();

        // When passing the Executable type, am I instantiating it?
        r.run(new Executable() {
            public void execute() {
                System.out.println("Executed!");
            }
        });

    }
}

How is this possible? Aren't you creating a new instance of interface Executable because of the new keyword? Is there something that I don't understand? A brief explanation and some examples are greatly appreciated!

MattGotJava
  • 185
  • 3
  • 14

2 Answers2

3

An interface is a class providing a template of methods needs to be implemented. You can't instantiate an interface as is because it would do nothing without its methods implementations.

If you instantiate an object which implements an interface, the methods' implementations are provided and works practically the same as you instantiate an interface with the same method's implementation. In your case, it's called an Anonymous Class which just provides the necessary implementation of methods to use. Having an interface:

interface Executable {
    void execute();
}

Instantiation is:

Executable executable = new Executable() {
    @Override
    public void execute() {
        System.out.println("Executed!");
    }
}

Here you provide the method execute() an implementation. Since the interface has only one method and acts as a functional interface, you can simplify it with a lambda expression since Java 8:

Executable executable = () ->  System.out.println("Executed!");
Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183
  • This is exactly what I needed! I'm now going to look into lambda expressions. – MattGotJava Jul 26 '18 at 18:11
  • I like lambda expressions personally. `r.run(() -> System.out.println("Executed!"));` looks better, right? :) You can even shorten all the `main` method to: `new Runner().run(() -> System.out.println("Executed!"));`. – Nikolas Charalambidis Jul 26 '18 at 18:14
1

Aren't you creating a new instance of interface Executable because of the new keyword? Answer : You are not instantiating the interface .In fact you are instantiating an anonymous inner class which implements Exceutable .

An example will be how to create a thread in java

Thread t= new Thread(new Runnable() {

            @Override
            public void run() {
                System.out.println("Hello World!");

            }
        });

In the Thread constructor we are passing an anonymous inner class which implements Interface Runnable

Debapriya Biswas
  • 1,079
  • 11
  • 23