userpal
userpal

Reputation: 1523

Why an instance of anonymous class can be assigned to an Interface variable?

I am new to Java. I have problem to understand the relation between anonymous class instance and interface. Refer to the example in this website:

// anonymous instance as a variable
Runnable r = new Runnable()
{
    @Override
    public void run()
    {
        //codes
    }
};

Thread t1 = new Thread(r, "anonymous 1");

// anonymous instance as a parameter
Thread t2 = new Thread (new Runnable()
{
    @Override
    public void run()
    {
        //codes
    }
}, "anonymous 2");

Based on the answers in this SO question (http://stackoverflow.com/questions/11069056/why-java-interface-can-be-instantiated-in-these-codes), I have a basic understanding of anonymous class.

However, I don't understand what is this:

Runnable r = new Runnable(){...};

On the right hand side, we created an instance of an anonymous class, so it is an object of a class. On the left hand side, it is an Interface variable.

Why an instance of anonymous class can be assigned to an Interface variable?

Refer to this website:

Thread(Runnable target, String name)

It seems that Thread is expecting the 1st argument to be an Interface variable.

Upvotes: 2

Views: 1502

Answers (2)

wattostudios
wattostudios

Reputation: 8764

This code...

Runnable r = new Runnable(){...};

The class Runnable is an interface, so you are correct that you can't create an instance of it directly. However, note the code in the {...} section - this is implementing the methods of the interface in a localised way.

Basically what is happening is you are creating an instance of the interface, which is only viewable and usable by the method where r is defined. As you have implemented all the methods in the {...} section, the class is valid and you can use it as you do any other Runnable object.

This code...

Runnable r = new Runnable()
{
    @Override
    public void run()
    {
        //codes
    }
};

Is effectively the same as doing the following...

public class MyRunnable implements Runnable {
    public void run()
    {
        //codes
    }
};

// This goes in your class where you want to create the Runnable object
Runnable r = new MyRunnable();

Both pieces of code will create a variable r that is a Runnable instance. The first solution is a shorthand for creating the instance, and is useful for something that won't need to be reused anywhere else. The second solution creates a separate class for implementing the interface, and it can be reused outside of the local scope.

We are able to store the variables with an interface type, provided that the object assigned to the variable implements the interface. So, in the code...

Runnable r = new Runnable(){...};

We are saying that r implements the Runnable interface. The bit in the {...} is where the interface is implemented, as explained earlier, so it all works out fine.

Upvotes: 4

Luiggi Mendoza
Luiggi Mendoza

Reputation: 85779

You can assign an object to an interface only if the object class implements that interface. In this case, you're creating an object of your anonymous class that implements the Runnable interface, making it a correct assignment.

Also, the Thread class has a non-arg constructor. That's why the code compiles.

Upvotes: 0

Related Questions