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

Java Problem Difficult to Understand

 
aadhar sharma
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

class Vehicle
{
public Vehicle()
{
System.out.println(" Vehicle ");
}
}

class Driver
{
public Driver()
{
System.out.println(" DRIVER ");
}
}

public class Car extends Vehicle
{
public static void main(String args[]) throws Exception
{
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("car.ser"));
out.writeObject(new Car());
out.flush();
out.close();
ObjectInputStream in = new ObjectInputStream(new FileInputStream("car.ser"));
Car c = (Car) in.readObject();
}
private Driver driver;

public Car()
{
System.out.println("Car");
}
}

The dont understand the output
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the output ?
What don't you understand about it ?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12145
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it's easier to help you if you tell us what the actual output is, and what output you are expecting. explain why you believe the output is wrong, and why you think it's what you think it is.
 
Pranav Pal
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, Aadhar , it would have been better if you have provided us the output
Anyways, this is my understanding of your program:

Whwn you call new Car(), first constructor of superclass Vehicle runs and prints Vehicle.Then constructor of class Vehicle runs and prints Car.

Now you do out.flush()
But out is an ObjectOutputStream, so Runtime environment tries to serealize the Car object.
But since Car doesn't implement Serializable, you get
the Exception:
NotSerializableException : Car
 
Bill Shirley
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course, the output should be



fix the code to be Serializable:



and then the output for me was


[ December 07, 2007: Message edited by: Bill Shirley ]
 
aadhar sharma
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Guys


But the output of

Vehicle
Car
Vehicle


I dont understand why the Vehicle is getting printed after the Car

Thanks in advance

Appreciate your help
 
Pranav Pal
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am also getting the same output after making Car Serializable:

Vehicle
Car
Vehicle


And I also don't understand why the last Vehicle is getting printed
 
Bob Ruth
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you deserialize an object.... take note of how it handles constructors for the object being built.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic