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!

2
  • 4
    It is called Anonymous class Commented Jul 26, 2018 at 17:40
  • This is called an anonymous class declaratio, not an instanciation Commented Jul 26, 2018 at 17:40

2 Answers 2

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!");
Sign up to request clarification or add additional context in comments.

2 Comments

This is exactly what I needed! I'm now going to look into lambda expressions.
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!"));.
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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.