aspose file tools*
The moose likes Beginning Java and the fly likes Java Problem Difficult to Understand Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java Problem Difficult to Understand" Watch "Java Problem Difficult to Understand" New topic
Author

Java Problem Difficult to Understand

aadhar sharma
Ranch Hand

Joined: Oct 09, 2006
Posts: 38
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


Thanks and Regards
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3646
    
  15
What is the output ?
What don't you understand about it ?


Joanne
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11402
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Pranav Pal
Ranch Hand

Joined: Nov 04, 2007
Posts: 74
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


Hakuna Matata!
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
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 ]

Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
aadhar sharma
Ranch Hand

Joined: Oct 09, 2006
Posts: 38
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

Joined: Nov 04, 2007
Posts: 74
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

Joined: Jun 04, 2007
Posts: 320
When you deserialize an object.... take note of how it handles constructors for the object being built.


------------------------
Bob
SCJP - 86% - June 11, 2009
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java Problem Difficult to Understand