GuruKulki
GuruKulki

Reputation: 26418

Can an interface extend the Serializable interface?

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

Answers (4)

Nischay Singla
Nischay Singla

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

akf
akf

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

Timothy
Timothy

Reputation: 2477

So can we do that?

Yes

will that extended interface will take the same effect as the Serilizable interface?

Yes

Upvotes: 8

Roman
Roman

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

Related Questions