• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java 5 Genrics

 
Arul Prasad
Ranch Hand
Posts: 57
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
is there any way to use Genrics in the ObjectOutputStream & ObjectInputSream. in Java 5.

My case wass I'm having a Animal class which need to be written in to one file and read it from the file.

If i am using the ObjectInputStream to read . I need to type cast it explicitly when i am saying

Animal a = (Animal)objectInputStream.readObject();



Can we use Genrics for avoiding this Type cast . Is any thing avail with java 5 if not why it was avoided ...?




 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you can't use generics to avoid that cast, because ObjectInputStream is not a generic class.

There's a good reason why it's not a generic class - a stream can contain (and usually does) many different kinds of objects, it wouldn't be very useful to have an ObjectInputStream that could read only one kind of object.
 
Srinivas Kalvala
Ranch Hand
Posts: 257
Firefox Browser Hibernate Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

The ObjectInputStream and OutputStream are used to read and write any type of object. When you are trying to readObject from the Stream it will return you the Object type and we have to type cast.

But we can't provide the generic to this one , and the reason is , it reads or writes any type of object. So we dont know which is next object. Just we have to check with type cast and it is safe too.

More inputs are welcome.
 
Arul Prasad
Ranch Hand
Posts: 57
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Srini

Yes I have also think that reason. but if Collection can use the Genrics . why can't this ObjectOutputStream and ObjectInputStream . Even Collection also can written in to the File. Which can contain the list of many type of Objects.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have never tried, so I don't know whether this will work, but:-

Try extending the ObjectInputStream and see whether you can convert it to a generic class.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Arul Prasad:
Yes I have also think that reason. but if Collection can use the Genrics . why can't this ObjectOutputStream and ObjectInputStream .


It could, but it would be of much less value.

When you have a List<Animal>, the compiler makes sure that only Animals are put into the list. So if you get an object from the list, you can be very sure that it *must* be an Animal.

When you have an ObjectInputStream<Animal>, the input is just a binary stream. The compiler can't guarantee that the stream was produced using an ObjectOutputStream<Animal>, so we actually don't know what the stream actually contains.

So, for a list, if you use generics consequently, you can be quite sure that you will never get a ClassCastException - the compiler would already find out about the problem. That's simply not possible for the streams, because you use two different objects to write and read the stream.

Does that sound reasonable?
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic