# Using Interface as Reference Type

Since an interface is a reference type, it can be used in the same way we have used instantiated objects from classes.

If the interface is use in this way, all the method will need to be implemented inline or anonymously.

## Example

Here is how we could have used the Listener interface to receive a message that we have connected to the chat server in the chat program. Notice the semicolon, ; at the end since this is a statement declaration.

We can now use the onConnected variable and pass it where the Listener parameter is required. In our case we can now use it to create a subscription so that we can be notified when our program have successfully connected to the chat server as follows :

mSocket.on(Socket.EVENT_CONNECT, onConnected);
mSocket.connect();


Notice the on method takes in a String parameter and a Listener. Its defined as follows :

    public Emitter on(String event, Emitter.Listener fn) {
//some code omitted
}


This is why we are able to pass in the onConnected variable since its of type Listener.

## Interface Parameters

If we are not going to use the interface variable in other place we can implement the interface anonymously. We can pass the interface body right in as a parameter and provide the method bodies inline.

mSocket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
@Override
public void call(Object... objects) {
System.out.println("Connected");
System.out.println(mSocket.id());