Java – why objectoutputstream Readobject() accesses MyClass readObject()

I'm reading the basics of Java io. I see that in order to use the custom writeobject function in a class, it must be declared private

private void writeObject(ObjectOutputStream oos);

Then, when we call

oos.writeObject(myClassObject);

This function finds the private writeobject method in MyClass and executes it

My question is: if this is true, won't a function violate the concept of data abstraction when it can call a private method of another class? What is the reason for this function?

Solution

I mostly make an informed guess here

Serialization works logically with a single object class It doesn't make much sense to inherit it, so it is forced to be private

It does violate some OO principles, but I can see why in this approach In particular, serialization has become heavily dependent on reflection

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>