Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 927665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T19:53:20+00:00 2026-05-15T19:53:20+00:00

I’ll try and explain my problem as concisely and clearly as possible. I feel

  • 0

I’ll try and explain my problem as concisely and clearly as possible. I feel that what I’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’s implied they exist in separate files].

// ----- SomeInterface.java
public interface SomeInterface extends Serializable
{
    public void setMsg(String msg);
    public void printMsg();
}

// ----- SomeImpl.java
public class SomeImpl implements SomeInterface
{
    private String msg;
    public void setMsg(String msg)
    {
        this.msg = msg;
    }


    public void printMsg()
    {
        System.out.println("Msg is: " +msg);
    }

}

Now I serialize an instance of [tt]SomeImpl[/tt]class:

...
    public void SomeMethInSomeClass()
    { 
        FileOutputStream fos = new FileOutputStream(new File("c:/test.ser"));
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        SomeInterface si = new SomeImpl();

        si.setMsg("blah blah");
        oos.writeObject(test);

        oos.close();
        fos.close();
        System.out.println("------ done -------");
    }

Now in a separate project, I have a JAR of the application above which contains the interface SomeInterface, but not the implementation class SomeImpl. In this application if I try to do the following:

....
....
    public void deserialiserMethod() throws Exception
    {
        FileInputStream fis = new FileInputStream(new File("c:/test.ser"));
        ObjectInputStream ois = new ObjectInputStream(fis);
        SomeInterface si = null;

        System.out.println("------ reading ----------");
        si = (SomeInterface) ois.readObject();
        si.printMsg();
        System.out.println("------- done ----------");
        ois.close();
        fis.close();
    }
....

… I get a ClassNotFoundException [for SomeImpl]. Now I know that my other project does NOT have the SomeImpl class in its classpath. But shouldn’t casting into a reference of the parent interface take care of that dependency?

thanks for your inputs!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T19:53:21+00:00Added an answer on May 15, 2026 at 7:53 pm

    This isn’t a question of casting, this is a question of the class type of the serialized object. It doesn’t matter if your code never refers directly to the SomeImpl class, the fact remains that the serialized object is still of that type.

    Consider this – what class would the deserialized object be an instance of, if not SomeImpl? The fields have to be defined somewhere, and if not in SomeImpl, then where?

    Even if you were to deserialize the object and cast it to java.lang.Object, you’d have to the same problem of what the actual representation and runtime type of the object is.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.