Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Serializable and Inheritance - How many objects?  RSS feed

 
Andy Middleditch
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been studying for the SCJP exam recently and I came across the following question:

Given:

10. class Car implements Serializable { }
11.
12. class Ford extends Car { }

If you attempt to serialize an instance of Ford, what is the result?

a. Compilation fails.
b. One object is serialized.
c. Two objects are serialized.
d. An exception is thrown at runtime.


So, I went and created a program to resolve the question with the two classes; one extending from the other, adding in some code to output an instantiation of class 'Ford':



import java.io.Serializable;
import java.io.ObjectOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class Main
{
public static void main(String [] args)
{
String filename = "c:\\test.ser";

if(args.length > 0)
{
filename = args[0];
}
Ford time = new Ford();

System.out.println(time.sCar);
FileOutputStream fos = null;
ObjectOutputStream out = null;

try
{
fos = new FileOutputStream(filename);
out = new ObjectOutputStream(fos);
out.writeObject(time);
out.close();
}
catch(IOException ex)
{
ex.printStackTrace();
}
}
}

class Car implements Serializable { private String sCar = "My Wonderful Car"; }
class Ford extends Car { public String sCar = "My Wonderful Ford"; }

Looking at the results of test.ser in a text editor, I can see both strings "My Wonderful Car" and "My Wonderful Ford" and also the names of the two classes - does this mean two objects are in the results of serialization (answer c) - or is it daft idea trying to make sense of a serialization in a text editor?
 
Rob Spoor
Sheriff
Posts: 21044
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to IO.
 
jack borton
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Andy,

I'm studying too for the SCJP examen. Thanks to your question, I could remember more things in the process of serialization.

The response to your question is that the JVM creates two objects. I'm going to divide the explanation in two things, objects created and things serialized. So lets do it !!



1. Objects created : In this topic, according to the JVM specification, when you try to instantiate a Ford object, talking in a technically way, the JVM load your object, the classes and interfaces that your object refers to, beginning for the super class and so on recursively. With this one you know at less the number of objects.

2. Things Serialized : In this topic, you get two objects serialized because they implements the Serializable interface. Although, if you define more reference variables (objects specifically), the Serialization process takes care of this one, making a graph of objects; Obviously, if you want to save the data, these objects must implements the Serializable interface.

I hope I response to your doubt. If there is something no clear, please don't hesitate to tell me.

Good luck!!
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm afraid I can't undnerstand what Jack is getting at, so I can't respond directly; nevertheless, I can give you the correct answer.

An instance of a class is like an onion. The innermost core is always the bits needed to comprise a java.lang.Object; each layer adds the bits for a derived class. So a Ford instance contains all the bits for an Object, and a Car, and the bits added for a Ford. Furthermore, to make sense of a Ford instance, the JVM needs the Ford class and the Car class and the Object class to all be loaded.

But if you create a Ford, it is a single object. There is no separate Car object, no separate Object object. What you see in your editor is the layers of the onion that make up a single Ford object.
 
jack borton
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, if I wasn't clear in the first topic. There is one object created, just I was talking about the process do it for the JVM.

Thanks Ernes.
 
Andy Middleditch
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest Friedman-Hill wrote:I'm afraid I can't undnerstand what Jack is getting at, so I can't respond directly; nevertheless, I can give you the correct answer.

An instance of a class is like an onion. The innermost core is always the bits needed to comprise a java.lang.Object; each layer adds the bits for a derived class. So a Ford instance contains all the bits for an Object, and a Car, and the bits added for a Ford. Furthermore, to make sense of a Ford instance, the JVM needs the Ford class and the Car class and the Object class to all be loaded.

But if you create a Ford, it is a single object. There is no separate Car object, no separate Object object. What you see in your editor is the layers of the onion that make up a single Ford object.


Thanks for your response. Yes, I understand the concept that a derived class (in this case Ford) will contain the components of the class that it is derived from (Car) - what I was surprised to see was:

1. The actual name of the super class in the serialization (Car)
2. The fact that the private member sCar (that is subsequently overridden and set to another value in the class Ford) appears with the original value "My wonderful Car" as well as the overridden value.

This confused me as to how many actual objects were contained in the serialization. But, as you say, one object it is...
 
John Kimball
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's probably why the original question didn't define members



 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!