Win a copy of Spring Boot in Practice this week in the Spring forum!
  • 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Nice Serialization Question

 
Ranch Hand
Posts: 430
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic