{"id":5268,"date":"2014-03-30T20:06:37","date_gmt":"2014-03-30T20:06:37","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/serialization-of-interface-reference-for-an-implementation-class-fails-at-de-serialization-collection-of-common-programming-errors\/"},"modified":"2014-03-30T20:06:37","modified_gmt":"2014-03-30T20:06:37","slug":"serialization-of-interface-reference-for-an-implementation-class-fails-at-de-serialization-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/serialization-of-interface-reference-for-an-implementation-class-fails-at-de-serialization-collection-of-common-programming-errors\/","title":{"rendered":"Serialization of Interface Reference for an Implementation Class fails at De-serialization-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;ll try and explain my problem as concisely and clearly as possible. I feel that what I&#8217;m trying might inherently be wrong, but somehow I believe it should work. So please feel free to treat this post as a mere clarification about the Serialization mechanism.<\/p>\n<p>So, I have an Interface implemented by a class [it&#8217;s implied they exist in separate files].<\/p>\n<pre><code>\/\/ ----- SomeInterface.java\npublic interface SomeInterface extends Serializable\n{\n    public void setMsg(String msg);\n    public void printMsg();\n}\n\n\/\/ ----- SomeImpl.java\npublic class SomeImpl implements SomeInterface\n{\n    private String msg;\n    public void setMsg(String msg)\n    {\n        this.msg = msg;\n    }\n\n\n    public void printMsg()\n    {\n        System.out.println(\"Msg is: \" +msg);\n    }\n\n}\n<\/code><\/pre>\n<p>Now I serialize an instance of [tt]SomeImpl[\/tt]class:<\/p>\n<pre><code>...\n    public void SomeMethInSomeClass()\n    { \n        FileOutputStream fos = new FileOutputStream(new File(\"c:\/test.ser\"));\n        ObjectOutputStream oos = new ObjectOutputStream(fos);\n        SomeInterface si = new SomeImpl();\n\n        si.setMsg(\"blah blah\");\n        oos.writeObject(test);\n\n        oos.close();\n        fos.close();\n        System.out.println(\"------ done -------\");\n    }\n<\/code><\/pre>\n<p>Now in a separate project, I have a JAR of the application above which contains the interface <code>SomeInterface<\/code>, but not the implementation class <code>SomeImpl<\/code>. In this application if I try to do the following:<\/p>\n<pre><code>....\n....\n    public void deserialiserMethod() throws Exception\n    {\n        FileInputStream fis = new FileInputStream(new File(\"c:\/test.ser\"));\n        ObjectInputStream ois = new ObjectInputStream(fis);\n        SomeInterface si = null;\n\n        System.out.println(\"------ reading ----------\");\n        si = (SomeInterface) ois.readObject();\n        si.printMsg();\n        System.out.println(\"------- done ----------\");\n        ois.close();\n        fis.close();\n    }\n....\n<\/code><\/pre>\n<p>&#8230; I get a <code>ClassNotFoundException<\/code> [for <code>SomeImpl<\/code>]. Now I know that my other project does NOT have the <code>SomeImpl<\/code> class in its classpath. But shouldn&#8217;t casting into a reference of the parent interface take care of that dependency?<\/p>\n<p>thanks for your inputs!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ll try and explain my problem as concisely and clearly as possible. I feel that what I&#8217;m trying might inherently be wrong, but somehow I believe it should work. So please feel free to treat this post as a mere clarification about the Serialization mechanism. So, I have an Interface implemented by a class [it&#8217;s [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-5268","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5268","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=5268"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5268\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=5268"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=5268"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=5268"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}