• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Java 5 Genrics

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ...?




 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 257
Hibernate Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 80653
476
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
reply
    Bookmark Topic Watch Topic
  • New Topic