File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes what is the output Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "what is the output" Watch "what is the output" New topic
Author

what is the output

m prabhu
Ranch Hand

Joined: Jan 29, 2008
Posts: 79

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

Joined: Aug 03, 2008
Posts: 9280
    
  17

the answer must be one object is serialized......


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
m prabhu
Ranch Hand

Joined: Jan 29, 2008
Posts: 79
how
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

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

Joined: Jan 29, 2008
Posts: 79
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

Joined: Aug 24, 2007
Posts: 12
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 ]

SCJP 5.0
vidhya suvarna
Ranch Hand

Joined: Aug 28, 2008
Posts: 148
Yeah since Car is implementing Serializable..and subclass will implicitly implement Serializable interface..
so it should have 2 objects serialized..


SCJP 1.4 - 88%<br />SCWCD 1.5 - Preparing
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8764
    
    5
Please post the entire question and keep discussing this - I don't think you've got it quite nailed down correctly yet.


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
m prabhu
Ranch Hand

Joined: Jan 29, 2008
Posts: 79
"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

Joined: Aug 07, 2008
Posts: 55
2 objects will be serialized. If you have the answer please post it.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

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

Joined: Feb 19, 2008
Posts: 2902
    
    1

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 !


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
ankit kumar
Ranch Hand

Joined: Apr 28, 2008
Posts: 106
ava.io.NotSerializableException will be thrown by the JVM


Cleared SCJP 1.5 | Cleared SCWCD 5.0
(SCEA/OCMJEA) in progress.....
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

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

Joined: Apr 28, 2008
Posts: 106
ya ankit its java.io.NotSerializableException
ankit kumar
Ranch Hand

Joined: Apr 28, 2008
Posts: 106
class Car implements Serializable { }
class Ford extends Car { }


i was replying for this one...
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8764
    
    5
Hey Guys,

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

Joined: Aug 27, 2008
Posts: 11
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

Joined: Aug 03, 2008
Posts: 9280
    
  17

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.....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: what is the output
 
Similar Threads
a bulk of questions ? from SUN's assessment exam
Object Serialization Doubt
serialization
Object type casting
Constructors called when deserializing properly serialized subclass instance