aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Serialization Code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Serialization Code" Watch "Serialization Code" New topic
Author

Serialization Code

Adeyemi Adeseye
Greenhorn

Joined: Jan 10, 2009
Posts: 27


If an instance of class Beagle is created, then Serialized, then deSerialized, what is the result?

SCJP 6.0 (83%)
saima kanwal
Ranch Hand

Joined: Oct 05, 2009
Posts: 148
When you serialize a Beagle object it will be serialized without any compiler error , because the subtypes of a serializable class are themselves serializable. when you deserialize it, then the constructor of Animal class will run because it is not serialized.


SCJP 6.0 (86%) SCWCD 5 (94%)
Adeyemi Adeseye
Greenhorn

Joined: Jan 10, 2009
Posts: 27
so what is the output of this program ?
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
Adeyemi, Why do you ask about the program's output?
As this is not a complete program as written, it has no
output. Please describe the problem you are trying to
solve.

Jim...


BEE MBA PMP SCJP-6
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

Adeyemi Adeseye wrote:so what is the output of this program ?

Don't you have a java compiler on which you can check what the output of the program is. If you don't understand the output then we can help you...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Just a reminder that serialization is NOT on the latest SCJP6 exam.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
raghu mrvreddy
Greenhorn

Joined: Nov 24, 2006
Posts: 7

public class Main {

public static void main(String[] args) throws Exception {
Animal be = new Beagle();

FileOutputStream f_out = new FileOutputStream( "myobject.data" );

// Write object with ObjectOutputStream
ObjectOutputStream obj_out = new ObjectOutputStream( f_out );

// Write object out to disk
obj_out.writeObject( be );

FileInputStream f_in = new FileInputStream( "myobject.data" );

// Read object using ObjectInputStream
ObjectInputStream obj_in = new ObjectInputStream( f_in );

// Read an object
Object obj = obj_in.readObject();
System.out.println( obj );
}

}

class Animal {

int i = 10;

Animal() {
System.out.print( "a" );
}

@Override
public String toString() {
return "i=" + this.i;
}
}

class Dog extends Animal implements Serializable {

int j = 11;

Dog() {
System.out.print( "d" );
}

@Override
public String toString() {

return super.toString() + "j=" + this.j;
}
}

class Beagle extends Dog {

int k = 12;

@Override
public String toString() {

return super.toString() + "j=" + this.j;
}
}

Check out...
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6662
    
    5

Please post your code inside code tags. They are hard to read without indentation


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Larry Chung
Ranch Hand

Joined: Feb 02, 2010
Posts: 247
Deepak Bala wrote:Please post your code inside code tags. They are hard to read without indentation


Yes, I also would appreciate the code tags. However, isn't it ironic that the Java code in SCJP exams are in very difficult to read format, with poor indentations?


SCJP 6
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Remember to flush and close your streams otherwise you could get unexpected behavior.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14276
    
  21

Please QuoteYourSources - from which book, mock exam or other source did you copy this question?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

Larry Chung wrote:isn't it ironic that the Java code in SCJP exams are in very difficult to read format, with poor indentations?

But not everyone who is here to help you going to give the SCJP exam, so generally its better for people to put their code in code tags if they want others to read their code and help them...
Larry Chung
Ranch Hand

Joined: Feb 02, 2010
Posts: 247
Ankit Garg wrote:
But not everyone who is here to help you going to give the SCJP exam, so generally its better for people to put their code in code tags if they want others to read their code and help them...


Yes I know and that is why I said, "I also would appreciate the code tags". I am glad JavaRanch has this feature for postings.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Serialization Code