• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Nice Serialization Question

 
Ranch Hand
Posts: 430
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice Serialization Question



What do you think is the output?
 
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Leandro Coutinho wrote:
What do you think is the output?



The code compiles fine, but gives InvalidClassException on running.
It runs without exception if the code which reads the serialized file is commented.
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would compile fine but would return a InvalidClassException when run.

When Dog(serializable sublass) is deserialised it would call the default constructor of its superclass because the superclass(Animal) isn't a Serializable Object.
Since the Animal class doesnt have a default constructor it would raise a flag and throw a suitable exception.

If you modify your code to look like this

then the code would not throw the exception when deserializing.
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember that although it is nice to know about, serialization is no longer on the current exam.
 
Leandro Coutinho
Ranch Hand
Posts: 430
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Wouter Oet wrote:Remember that although it is nice to know about, serialization is no longer on the current exam.


Thanks for pointing out. I didn't know that. I just looked the Exam Topics and it's not there


Annotations are not on the exam too. And these two are important topics.
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Accept for @Override, @SupressWarnings and @Deprecated, annotations aren't used often in Java SE. They're used very often in Java EE but the Java EE exams cover those topics in detail.
 
You can't have everything. Where would you put it?
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic