Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Serialization Code

 
Adeyemi Adeseye
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


If an instance of class Beagle is created, then Serialized, then deSerialized, what is the result?
 
saima kanwal
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Adeyemi Adeseye
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so what is the output of this program ?
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Ankit Garg
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just a reminder that serialization is NOT on the latest SCJP6 exam.
 
raghu mrvreddy
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post your code inside code tags. They are hard to read without indentation
 
Larry Chung
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember to flush and close your streams otherwise you could get unexpected behavior.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15369
40
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please QuoteYourSources - from which book, mock exam or other source did you copy this question?
 
Ankit Garg
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic