• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Serialization

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Source : Whiz Lab


When deserailzing there is a runtime error saying missing default constructor...
So does that mean that every time an object is created then the default constuctor is invoked irrespective of how the actual object is created(one argument)??
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is that when an object of a class which is serializable (but which has a superclass which is not serializable) is deserialized, then the instance initialization code for the superclass (including a call to a no-args constructor) will run. In your case, since B is not serializable, when the object of type A is deserialized, the JVM will attempt to call the B() constructor, but since you haven't coded it (and because you have coded another constructor) the class doesn't have one, which is what is giving you the issue.
 
Kedar Nath
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But the object has used the one argument constructor when it is actual created... So why it is not using the one argument constructor again???
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are deserializing an object of class A, which you constructed using the constructor that takes 1 arg. The constructor for A is not run when you deserialize it, because that's the point of serialization. You just copy the state of object A from the serialization file. The problem is that not all of the object's state got serialized. Since A extends B and B is not serializable, when you serialized the instance of A, the part of the object state that is inherited from class B did not get serialized. The way Java fixes that is by running the default instance initialization code for objects of class B, and that includes a call to the no-arg constructor B(). But because B doesn't have that constructor, you get a runtime exception.

This is explained very well in the K&B book, do you have it?
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what Ruben said is correct but you are creating object of the serializable class from which you are invoking the overloaded superclass constructor with super(x) but during deserialization it will get the serialized object but this one depends on the superclass "JVM will invoke Superclass constructer i.e no args one "
 
Kedar Nath
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for clearing the doubt

Ruben Soto wrote:This is explained very well in the K&B book, do you have it?


May be i overlooked it...
 
Kedar Nath
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Created a new topic.
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kedar Nath wrote: i more doubt...
What is the difference between JVM thrown exception and programmatically thrown exception...
the difference is kind of blur...
LIke the IllegalArgumentException(Programmatically), which is thrown when the method receives arg differently formated.But the method is actual receiveing the argument which is sent by the us!!
Similarly, ClassCastException(JVM), when coding we did the casting in a wrong manner.So it should be programmtically thrown..

But StackOverflowError, sound correct i mean as a JVM thrown..

Please dont if i sound stupid ...
Please explain


Kedar, it's better if you start another topic for this question, since it is a new question and that way more people will be able to answer you.
 
Message for you sir! I think it is a tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic