Win a copy of Head First Android this week in the Android forum!
  • 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 Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Incompatible Types

 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have extended TreeSet and added a method to get the text from a TextField.
I want to serialize it. When I go to cast the type to the extended class which is the same thing, I get the below error message.



found : java.util.TreeSet
required: film_titles.ActorList
ActorList list = (TreeSet)obInputStream.readObject();

 
Marshal
Posts: 74365
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . well, you are casting it to a TreeList . . .

Are you trying to serialize or deserialize the object in this method?

I can't remember exactly how you are supposed to serialize an object, but it goes something like


and you use this method as a method of the object you wish to serialize.

If I remember correctly, and I haven't done it for some time, so neither of my code suggestions will be at all accurate, but to get the object back you have to call a static method of the class of the object you want to get back, which goes something like:

or something along those lines.
You usually use the serialize and deserialize methods from within the same object and class respectively.

At least I think that's how it is supposed to work . . .

CR
[ October 15, 2005: Message edited by: Campbell Ritchie ]
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can cast the subclass to the superclass, but not the other way around.

In your example, casting an ActorList to a TreeSet would be acceptable.

Think about it this way: your subclass could implement more methods and data elements than the base class. If you then cast the base class to the subclass, where are the implementations of those methods and data elements going to come from?
 
author
Posts: 23909
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer is a bit more subtle... and actually, the problem is not the cast.



The object returned is an "Object" reference, that may be casted to an "ActorList". Since the ActorList class is inherited from the TreeSet class, in this case, it may also be casted to a TreeSet reference. The cast is totally valid.

It is the assignment that is not valid. The ActorList class is a subclass of the TreeSet class -- an implicit cast in not possible here. For the assignment to work, the object must be casted to a ActorList object.

Henry
 
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Assuming ActorList extends TreeSet the above would be legal though.
More defined things can go inside of less defined things... but you can't put a vaguer object inside of a more specific thing (with out casting in which case it better really be referring to what you think its going to refer to or you will get a runtime error).
 
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tad Dicks:

Assuming ActorList extends TreeSet the above would be legal though.
More defined things can go inside of less defined things... but you can't put a vaguer object inside of a more specific thing (with out casting in which case it better really be referring to what you think its going to refer to or you will get a runtime error).



thats is quite correct.
though I'd prefer
if you have other fields serialized. And obviously if you serialied something it probably means, you need it back right? so better get it into an ActorList.
And another question if you don't mind. Why have you put this in a constructor?
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic