In the Android documentation here: http://developer.android.com/guide/components/fragments.html A Fragment implements an interface.
In the onAttach() callback, it seems to cast the current Activity to an interface. Conceptually, how is this possible and is the same type of cast standard practice in vanilla Java?
public static class FragmentA extends ListFragment {
// Container Activity must implement this interface
public interface OnArticleSelectedListener {
public void onArticleSelected(Uri articleUri);
OnArticleSelectedListener mListener;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnArticleSelectedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement OnArticleSelectedListener");
}
}
...
}
A type cast—or simply a cast— is an explicit indication to convert a value from one data type to another compatible data type. A Java interface contains publicly defined constants and the headers of public methods that a class can define.
The Android Interface Definition Language (AIDL) is similar to other IDLs you might have worked with. It allows you to define the programming interface that both the client and service agree upon in order to communicate with each other using interprocess communication (IPC).
Some basic knowledge about the topic:
Think about an interface
as a bundle of functions. If a class
implements an interface
, then it guarantees, that it has all the interfaces functions implemented.
In your case:
If you have an object and you don't need more than the functions of an interface, that your object implements, then you can treat (and cast) that object to that interface. This way you loose "information" about your object, because you won't be able to use its functions (except the interface functions), but sometimes its enough.
Any object that is created from a class which implements some interface is also an instance of that interface.
Consider this:
public class Main {
public static void main(String[] args) {
MyClass mine = new MyClass();
checkIsFoo(mine);
}
public static void checkIsFoo(MyClass mine) {
System.out.print(mine+"");
if (mine instanceof MyFoo)
System.out.println(" is Foo!");
else
System.out.println(" is not Foo!");
}
public static interface MyFoo {
//
}
public static class MyClass implements MyFoo {
//
}
}
In this case, the following is printed out: Main$MyClass@52c8c6d9 is Foo!
. This indicates that the MyClass
object is also an instance of MyFoo
. Since they are shared instances, calling a cast to (MyFoo) mine
would also be allowed.
This is, as @antlersoft said, part of vanilla Java (and can be seen here).
Because, in this case, the Activity
passed to onAttach
should always be the object which is both the Activity
and the OnArticleSelectedListener
, casting it to OnArticleSelectedListener
should always succeed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With