import java.io.*;

public class SerializationProblem {

    @FunctionalInterface
    public interface MyFunctionalInterface extends Serializable {
        int doApply();
    }

    static class MySuperclass implements Serializable {
        public int returnValue() {
            return 1;
        }
    }

    static class MyImplementation extends MySuperclass {
    }

    static class MyOtherImplementation extends MySuperclass {
    }

    static class SuccessfullDeserializable implements Serializable {
        private final MyFunctionalInterface myFunctionalInterface;
        private final MyFunctionalInterface myOtherFunctionalInterface;

        SuccessfullDeserializable() {
            final MyImplementation myImplementation = new MyImplementation();
            this.myFunctionalInterface = myImplementation::returnValue;
            final MyImplementation myOtherImplementation = new MyImplementation();
            this.myOtherFunctionalInterface = myOtherImplementation::returnValue;
        }
    }

    static class FailingDeserializable implements Serializable {
        private final MyFunctionalInterface myFunctionalInterface;
        private final MyFunctionalInterface myOtherFunctionalInterface;

        FailingDeserializable() {
            final MyImplementation myImplementation = new MyImplementation();
            this.myFunctionalInterface = myImplementation::returnValue;
            final MyOtherImplementation myOtherImplementation = new MyOtherImplementation();
            this.myOtherFunctionalInterface = myOtherImplementation::returnValue;
        }
    }

    private static Object serializeAndDeserialize(final Serializable serializable) throws IOException, ClassNotFoundException {
        final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        final ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
        objectOutputStream.writeObject(serializable);
        objectOutputStream.flush();
        final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
        final ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
        return objectInputStream.readObject();
    }

    public static void main(final String[] args) throws Exception {
        final Object first = serializeAndDeserialize(new SuccessfullDeserializable() {});
        System.out.println(first.getClass().getName() + " successfully deserialized");

        final Object second = serializeAndDeserialize(new FailingDeserializable() {});
// not reached....
        System.out.println(second.getClass().getName() + " successfully deserialized");
    }
}