import java.io.*;

public class Test {
    public interface IFoo extends Serializable {
        void foo();
    }

    public interface Marker2 {
    }

    public interface Marker1 {
    }

    public static void foo() {
    }

    public static void main(String[] args) throws IOException, ClassNotFoundException {
        Object x1 = (IFoo & Marker1) Test::foo;
        Object y1 = roundtrip(x1);

        Object x2 = (IFoo & Marker2) Test::foo;
        Object y2 = roundtrip(x2);

        check(y1, "y1");
        check(y2, "y2");
        System.out.println(y1==y2);
    }

    private static void check(Object y, String name) {
        if (y instanceof Marker1) {
            System.out.println(name + " IS Marker1");
        } else {
            System.out.println(name + " IS NOT Marker1");
        }

        if (y instanceof Marker2) {
            System.out.println(name + " IS Marker2");
        } else {
            System.out.println(name + " IS NOT Marker2");
        }
    }

    private static Object roundtrip(Object x) throws IOException, ClassNotFoundException {
        ByteArrayOutputStream out1 = new ByteArrayOutputStream();
        new ObjectOutputStream(out1).writeObject(x);
        return new ObjectInputStream(new ByteArrayInputStream(out1.toByteArray())).readObject();
    }
}