Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Fahman Khan
3,798 PointsInstantiating objects of interfaces?
From what i recall, i remember that you can never create an instance of an interface; however, in the following code
myCall.enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
}
@Override
public void onResponse(Response response) throws IOException
{
try {
if(response.isSuccessful())
{
Log.v(TAG, response.body().string());
}
} catch(IOException e)
{
Log.e(TAG, "Exception Caught",e);
}
}
});
In myCall.enqueue(new Callback()...
Callback is an interface. My question is what is going on here and why is an object of an interface being created?
2 Answers
Dan Johnson
40,533 PointsWhat's happening is an anonymous class is being created, and that class is implementing the Callback interface. Basically it's a one-off class definition you're working with that defines the body of the methods so it can actually be instantiated.
An easier way to figure out what is going on is to look at a private inner class that does the same thing:
public class MovieActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
// ...
call.enqueue(new CallbackImplementation());
}
private class CallbackImplementation implements Callback {
@Override
public void onFailure(Request request, IOException e) {
// ...
}
@Override
public void onResponse(Response response) {
// ...
}
}
}
I prefer this method to anonymous classes myself, it's a much cleaner separation of logic.
Fahman Khan
3,798 PointsUnderstood! This leads me to another question. This is the following defination of the enqueue method.
public void enqueue(Callback responseCallBack)
Callback is an interface and we pass some anonymous object that implements the Callback interface. How is Java allowing me to create some other anonymous object when the parameter of this is Callback. Can you clear this concept? :)
Dan Johnson
40,533 PointsWhen a class implements an interface it's guaranteeing that it has the ability to perform the methods of that interface. Given that, you can treat anything implementing that interface as the interface itself.
This has the advantage of letting you deal with many types of classes in the same way. For example if you're trying to sort something, you should only need to worry about if you can compare the class you're dealing with rather than all the implementation details. You can do this by accepting anything that implements Comparable.
Dan Johnson
40,533 PointsDan Johnson
40,533 PointsAdded code tags.