Hi,
I'm at the tail end of my course and it touched briefly on the serializable interface that enables objects to be saved to file as binary data.
Don't worry, what I'm about to ask has nothing to do with my course, the assignemts are done and i'm just waitng for the exam!
Right now that the caveat is out the way....
part of the info on serialised objects was that you can use getClass to obtain an objects class so you don't even need to know what types of object are stored.
That's all they said on the subject and no examples were given, so I thought I'd have a play myself as I found this intriguing. However I have been unable to get any code to compile. I sent my example to my tutor but they have also been unable to come up with a working solution and so hoped you guys could.
Senario...
You have a binary file of serialised objects, they were stored as a Set of objects but you don't know what class they are, you just know they can all understand the method mySpecialMethod and you wish to execute it against each object.
As you don't know the class of each set element, I'm guessing you have to use class Object to load them... however if I then try to cast the object to then execute the required method I get a complile error...
Which is telling me the compiler is not seing the cast take place and so is unable to get access to the correct class's message protocol, how would you do the following and get it to compile...
Is it possible to dynamically cast objects and then run instance methods from their class protocol?
"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."
"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"
Google Rank Extractor -> Perl beta with FusionCharts
I'm at the tail end of my course and it touched briefly on the serializable interface that enables objects to be saved to file as binary data.
Don't worry, what I'm about to ask has nothing to do with my course, the assignemts are done and i'm just waitng for the exam!
Right now that the caveat is out the way....
part of the info on serialised objects was that you can use getClass to obtain an objects class so you don't even need to know what types of object are stored.
That's all they said on the subject and no examples were given, so I thought I'd have a play myself as I found this intriguing. However I have been unable to get any code to compile. I sent my example to my tutor but they have also been unable to come up with a working solution and so hoped you guys could.
Senario...
You have a binary file of serialised objects, they were stored as a Set of objects but you don't know what class they are, you just know they can all understand the method mySpecialMethod and you wish to execute it against each object.
As you don't know the class of each set element, I'm guessing you have to use class Object to load them... however if I then try to cast the object to then execute the required method I get a complile error...
cannot find symbol - method mySpecialMethod()
Which is telling me the compiler is not seing the cast take place and so is unable to get access to the correct class's message protocol, how would you do the following and get it to compile...
Code:
public void myTest()
{
Set<Object> recoveredSet = new HashSet<Object>((Set)ObjectIO.retrieveObject());
for(Object obj : recoveredSet)
{
System.out.println(obj.getClass().cast(obj).mySpecialMethod());
}
}
Is it possible to dynamically cast objects and then run instance methods from their class protocol?
"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."
"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"
Google Rank Extractor -> Perl beta with FusionCharts