importjava.io.Serializable;importjava.io.*;publicclassTestDeserialization {interfaceAnimal {publicvoideat();}//Class must implements Serializable to be serializablepublicstaticclassCatimplementsAnimal,Serializable {@Overridepublicvoideat() {System.out.println("cat eat fish");}}//Class must implements Serializable to be serializablepublicstaticclassDogimplementsAnimal,Serializable {@Overridepublicvoideat() {try {Runtime.getRuntime().exec("calc");} catch (IOException e) {e.printStackTrace();}System.out.println("dog eat bone");}}//Class must implements Serializable to be serializablepublicstaticclassPersonimplementsSerializable {privateAnimal pet;publicPerson(Animal pet){this.pet= pet;}//readObject implementation, will call the readObject from ObjectInputStream and then call pet.eat()privatevoidreadObject(java.io.ObjectInputStream stream)throwsIOException,ClassNotFoundException {pet = (Animal) stream.readObject();pet.eat();}}publicstaticvoidGeneratePayload(Object instance,String file)throwsException {//Serialize the constructed payload and write it to the fileFile f =newFile(file);ObjectOutputStream out =newObjectOutputStream(new FileOutputStream(f));out.writeObject(instance);out.flush();out.close();}publicstaticvoidpayloadTest(String file) throwsException {//Read the written payload and deserialize itObjectInputStream in =newObjectInputStream(new FileInputStream(file));Object obj =in.readObject();System.out.println(obj);in.close();}publicstaticvoidmain(String[] args) throwsException {// Example to call Person with a DogAnimal animal =newDog();Person person =newPerson(animal);GeneratePayload(person,"test.ser");payloadTest("test.ser");// Example to call Person with a Cat//Animal animal = new Cat();//Person person = new Person(animal);//GeneratePayload(person,"test.ser");//payloadTest("test.ser");}}