Otmàane Fikri
Otmàane Fikri

Reputation: 109

Difference between implementing an interface and creating an instance of it

I want to know what is the difference between implementing interface and creating an instance of it, here is an example of code, this code allows to Communicating between IntentService and Activity using ResultReceiver.

MySimpleReceiver Class :

public class MySimpleReceiver extends ResultReceiver {

public static final Creator CREATOR = null;
private Receiver receiver;

// Constructor takes a handler
public MySimpleReceiver(Handler handler) {
    super(handler);
}

// Setter for assigning the receiver
public void setReceiver(Receiver receiver) {
    this.receiver = receiver;
}

// Defines our event interface for communication
public interface Receiver {
    void onReceiveResult(int resultCode, Bundle resultData);
}

// Delegate method which passes the result to the receiver if the receiver
// has been assigned
@Override
protected void onReceiveResult(int resultCode, Bundle resultData) {
    if (receiver != null) {
        receiver.onReceiveResult(resultCode, resultData);
    }
}
}

which is best way to Setup the callback for when data is received from the service :

implementing MySimpleReceiver.Receiver Interface in the MainActivity:

public class MainActivity extends Activity implements MySimpleReceiver.Receiver 

    {

        @Override
        public void onReceiveResult(int resultCode, Bundle resultData) {
            //
        }
    }

or creating an instance of the interface in the MainActivity:

public class MainActivity extends Activity {


    public MySimpleReceiver receiverForSimple;


    // Setup the callback for when data is received from the service
    public void setupServiceReceiver() {
        receiverForSimple = new MySimpleReceiver(new Handler());
        // This is where we specify what happens when data is received from the
        // service
        receiverForSimple.setReceiver(new MySimpleReceiver.Receiver() {
            @Override
            public void onReceiveResult(int resultCode, Bundle resultData) {
                if (resultCode == RESULT_OK) {
                    String resultValue = resultData.getString("resultValue");
                    Toast.makeText(MainActivity.this, resultValue, Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

}

and what is the difference between? thanks

Upvotes: 1

Views: 499

Answers (2)

Shubham
Shubham

Reputation: 531

You Can not create the instance of interface. You can only create the instance of class which implement that interface. Example :

interface Calculate{
    void doCalculation();
}

class Sample implements Calculate{
   @Override
   void doCalculation(){
   //TODO Write your code to calculate
  }
}

//=======================================//

Calculate calculate = new Sample();
calculate.doCalculation();

Upvotes: 0

Jeff Watkins
Jeff Watkins

Reputation: 6359

You cannot "create an instance" of an interface, only an instance of a class which implements it.

An interface is nothing more than a tightly binding contract, which you must fully implement.

A class is a realised piece of functionality.

The example you provided shows that you're overriding MySimpleReceiver.Receiver in an anonymous class. It looks like you're instantiating an interface, but you are not. This class has no name (hence anonymous) but its implementation is contained within the braces following the new statement.

Anonymous classes are hugely useful, but difficult to read, especially if you're used to older versions of Java or other languages which don't support this kind of code.

Please see the tutorial on anonymous classes here

You would use the first example (direct implementation) if you wanted to reuse the class in any way. It makes your solution somewhat larger though.

The second example is great if you've got a single use and don't want a proliferation of class files in your solution.

Upvotes: 3

Related Questions