Reputation: 26418
Is it possible to create an interface extending the Serializable interface?
If yes, will that extended interface behave like the Serilizable interface? If not, why?
Upvotes: 22
Views: 23470
Reputation: 11
I am explaining this by a situation often encountered in android. If you want to pass the instance of a custom listener(interface), to fragment, then the listener(interface) extend the Serializable can be a option for you. eg
Suppose there is an Interface :public interface OnDurationChangeListener extends Serializable {
public void onDurationChange(Duration duration);
}
from an Activity I want to Export the instance Listener(interface) to Fragment.
bundle.putSerializable(ARGUMENT_LISTENER, new OnDurationChangeListener() {
@Override
public void onDurationChange(Duration duration) {
// some code
}
});
And in fragment you can Get this Listener instance as:
mListener = (OnDaysSelectListener) getArguments().getSerializable(ARGUMENT_LISTENER);
And from the fragment I can Call the Callback method in activity as
mListener.onDaysSelect(mWeeKDayList);
Upvotes: 1
Reputation: 39485
Yes, you can extend the Serializable
interface. If you do, all classes that implement the new subinterface will also be implementing Serializable
.
Upvotes: 29
Reputation: 2477
So can we do that?
Yes
will that extended interface will take the same effect as the Serilizable interface?
Yes
Upvotes: 8
Reputation: 66166
Yes, it's normal. I did it once when decided that all classes in my domain should be serializable. They implemented some interface already so I simply extended that interface from Serializable (as you describe).
Upvotes: 3