Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what is the output

 
m prabhu
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

from sun preevaluation test



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.
 
Ankit Garg
Sheriff
Posts: 9518
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the answer must be one object is serialized......
 
m prabhu
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how
 
Ankit Garg
Sheriff
Posts: 9518
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
because a Ford IS-A car. So every instance members of class Car will also be saved if you serialize an object of class Ford...

Two objects will be serialized if the structure was like this

class Car implements Serializable{}

class Ford implements Serializable
{
Car car = new Car();
}

Now serializing an object of Ford will serialize two objects(the 2nd one will be inside the first one...)
 
m prabhu
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but since 'Ford' extends 'Car' which is a serilized class it(Ford) implicitly is also serilized..so my opinoin is even in this case also we will get two objects serialized. this is my understanding.but i too have doubt.
Could anyone throw more light on this.
 
Marco Piva
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The superclass is not part of the object graph.
Otherwise, when you try to serialize an object of a serializable class that extends a non-serializable class, would get a java.io.NotSerializableException
[ August 29, 2008: Message edited by: M. Piva ]
 
vidhya suvarna
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah since Car is implementing Serializable..and subclass will implicitly implement Serializable interface..
so it should have 2 objects serialized..
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post the entire question and keep discussing this - I don't think you've got it quite nailed down correctly yet.
 
m prabhu
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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?
Compilation fails.
One object is serialized.
Two objects are serialized.
An exception is thrown at runtime.
"
this is the entire question i got in sun's preevaluation test in sun website.
Since there is no description for answers;for questions which i felt difficult to solve i posted here.
 
V K Gupta
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
2 objects will be serialized. If you have the answer please post it.
 
Ankit Garg
Sheriff
Posts: 9518
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think one object will be serialized....If it was a Has-A relationship then two objects would have been serialized....

Lets take an example

Let serialization be getting into a Garage,

Now when Ford get's serialized (i.e. gets into the garage), then only Ford will get serialized, why would Ford and Car get serialized....

I think I am correct but let's see what others have to say...
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by m prabhu:

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


"an instance is serialized " so definitely only one instance of class Ford is serialized ! This what I thought !
 
ankit kumar
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ava.io.NotSerializableException will be thrown by the JVM
 
Ankit Garg
Sheriff
Posts: 9518
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by kumar ankit:
ava.io.NotSerializableException will be thrown by the JVM


it is java.io.Not.........

And what are you talking about...if you are replying to some earlier replies please quote it.....

If you are giving answer to the original question....then my friend you are wrong....Ford indirectly inherits serializable....so it can be serialized...
[ August 30, 2008: Message edited by: Ankit Garg ]
 
ankit kumar
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ya ankit its java.io.NotSerializableException
 
ankit kumar
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Car implements Serializable { }
class Ford extends Car { }


i was replying for this one...
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Guys,

What about class Object? Doesn't that fit in here somehow?
 
Gustavo Villegas
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by kumar ankit:
ava.io.NotSerializableException will be thrown by the JVM



You don't get that exception.



I think the answer is, One object is serialized. Because only one object instanced,even if it extends from car, therefore only one object can be serialized.
 
Ankit Garg
Sheriff
Posts: 9518
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bert Bates:
Hey Guys,

What about class Object? Doesn't that fit in here somehow?


Hmmm...Good question.... Well as it is written in your book, if you serialize an object of a class, the fields of its non-serializable super class(and it's super classes) are not serialized. The constructor of the first non-serializable super class(and it's super classes) of the class whose object is being de-serialized run.....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic